How to convert datetime from time ago in php

Webee Cafe
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');


Latest Posts

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.

Mysore Palace

Mysore Palace

The Mysore Palace, also known as the Amba Vilas Palace, is a historical palace located in the city of Mysore in the southern state of Karnataka, India. It is one of the most famous tourist attractions in the country and is renowned for its magnificent architecture and rich cultural heritage.

All about the King Cobra - 2 incidents, Power, Venom, Style, Speed

All about the King Cobra - 2 incidents, Power, Venom, Style, Speed

We all know that the king of the jungle is the lion. Because of its strength, majestic gait and roar that shakes the entire forest. Similarly, how much do you know about the snake king? Yes, its name itself says Raja Naga. That means King Cobra. Its scientific name is called ophiophagus hanna. It is the world's largest venomous snake. Its poison is so dangerous that once the poison from its mouth is distributed to 10 people, ten people will lose their lives.

Leave a reply

Exevor's Affordable Elegance: Top Picks for Budget-Savvy Shoppers

Discover unbeatable deals at Exevor Shop's featured product section! Explore a curated selection of budget-friendly yet high-quality items for the savvy shopper in you.