How to convert datetime from time ago in php

How to convert datetime from time ago in php

Simple php function for converting datetime format to Time Ago format in php

Create a function get_timeago()

 function get_timeago($time) {
    // Calculate difference between current
    // time and given timestamp in seconds
    $curtim=date_timestamp_get(date_create(date('Y-m-d H:i:s')));
    $diff = $curtim - strtotime($time);
    // Time difference in seconds
    $sec = $diff;
    // Convert time difference in minutes
    $min = round($diff / 60 );
    // Convert time difference in hours
    $hrs = round($diff / 3600);
    // Convert time difference in days
    $days = round($diff / 86400 );
    // Convert time difference in weeks
    $weeks = round($diff / 604800);
    // Convert time difference in months
    $mnths = round($diff / 2600640 );
    // Convert time difference in years
    $yrs = round($diff / 31207680 );
      
    // Check for seconds
    if($sec <= 60) {
        return "$sec seconds ago";
    }
      
    // Check for minutes
    else if($min <= 60) {
        if($min==1) {
            return "one minute ago";
        }
        else {
            return "$min minutes ago";
        }
    }
      
    // Check for hours
    else if($hrs <= 24) {
        if($hrs == 1) { 
            return "an hour ago";
        }
        else {
            return "$hrs hours ago";
        }
    }
      
    // Check for days
    else if($days <= 7) {
        if($days == 1) {
            return "Yesterday";
        }
        else {
            return "$days days ago";
        }
    }
      
    // Check for weeks
    else if($weeks <= 4.3) {
        if($weeks == 1) {
            return "a week ago";
        }
        else {
            return "$weeks weeks ago";
        }
    }
      
    // Check for months
    else if($mnths <= 12) {
        if($mnths == 1) {
            return "a month ago";
        }
        else {
            return "$mnths months ago";
        }
    }
      
    // Check for years
    else {
        if($yrs == 1) {
            return "one year ago";
        }
        else {
            return "$yrs years ago";
        }
    }
}
}

Then call function like this

echo  get_timeago('2021-07-01 17:05:25');


Leave a reply

Latest Posts

10 Best Malayalam Movies You Must Watch in 2024

10 Best Malayalam Movies You Must Watch in 2024

The Malayalam film industry is renowned for producing inventive and varied films, and 2024 is already looking to be no different. With so many different films slated for release—from high-profile blockbusters to indie productions—there's something for everyone to enjoy. Here we have listed the 10 best Malayalam movies that give you goosebumps and make you cry. Most of the Malayalam movies are written based on true stories and events.

Trend: Exevor launched new tool for domain availability check

Trend: Exevor launched new tool for domain availability check

We launched a tool for search domain name for check availability. This tool is little bit straight and quick.

Kiwifruit

Kiwifruit

Kiwifruit, also known simply as kiwi, is a small, fuzzy fruit with a brown, hairy skin and bright green flesh. It is known for its unique, sweet-tart flavor and vibrant green color. The scientific name for the most common variety is Actinidia deliciosa. Kiwifruit is native to China and was originally called Chinese gooseberry, but it was later renamed kiwifruit or kiwi in reference to the New Zealand national bird, the kiwi, which has a similar brown, fuzzy exterior.

Golden Temple Kushalnagar, Coorg's

Golden Temple Kushalnagar, Coorg's

The Golden Temple in Kushalnagar, Coorg, is officially known as the Namdroling Monastery. It is a significant Tibetan Buddhist monastery and is one of the largest teaching centers of the Nyingma lineage of Tibetan Buddhism outside Tibet.