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
Smallest Cricket Stadium In The India By Boundary
Smallest Cricket Stadium In The India By Boundary

Cricket is more than just a sport in Indiaβ€”it’s a passion! From big stadiums like Wankhede and Eden Gardens to smaller, cozier grounds, each has its own charm. But have you ever wondered which is the smallest cricket stadium in India? In this guide, we’ll explore the top 12 "smallest cricket stadiums in the India by boundary", their unique features, and why they’re special. Whether you're a cricket fan or just curious, this fun and easy-to-read article will give you all the details!

 Best Fielder in the World of All Time
Best Fielder in the World of All Time

In cricket, fielding is a genuine skill. Over the years, I have seen hundreds of matches across formatsβ€”Test, ODI, and T20. From stunning dives to lightning-fast reactions, I've always been astonished by players who rescue valuable runs and make unbelievable catches. That is why I chose to discuss the "Best fielder in the world of all time". Using data, personal experiences, and a passion for the game, this blog will guide you over the field of fielding.

Asia Cup 2025 Schedule:
Asia Cup 2025 Schedule:

Hey there, cricket fans! Sharing what I know about the Asia Cup 2025 makes me quite happy.

Whatever the Bishnoi community says, it is difficult for Salman Khan to apologize in the black buck case
Whatever the Bishnoi community says, it is difficult for Salman Khan to apologize in the black buck case

Salman Khan has been advised by the Bishnoi community of Rajasthan to apologize in the blackbuck poaching case - but will the matter end if Salman Khan does so? And will Salman Khan want to take this path?