How to create time difference like 1 day ago in php??


3 Votes, Rating: 4.671 Star2 Stars3 Stars4 Stars5 Stars

Tags:

Time difference function to display particular records added x mins ago!

Calculate the difference in time between a past date and a future date

A time difference function that outputs the time passed in facebook’s style: 1 day ago, or 4 months ago.

This is really cool function, you dont need to do any just pass value( your db date) to this function and it returns “ago time”. easy isn’t it ?

 
<?php
function Agotime($date)
{
    if(empty($date)) {
        return "No date provided";
    }
 
    $periods         = array("second", "minute", "hour", "day", "week", "month", "year", "decade");
    $lengths         = array("60","60","24","7","4.35","12","10");
 
    $now             = time();
    $unix_date         = strtotime($date);
 
       // check validity of date
    if(empty($unix_date)) {    
        return "Bad date";
    }
 
    // is it future date or past date
    if($now > $unix_date) {    
        $difference     = $now - $unix_date;
        $tense         = "ago";
 
    } else {
        $difference     = $unix_date - $now;
        $tense         = "from now";
    }
 
    for($j = 0; $difference >= $lengths[$j] && $j < count($lengths)-1; $j++) {
        $difference /= $lengths[$j];
    }
 
    $difference = round($difference);
 
    if($difference != 1) {
        $periods[$j].= "s";
    }
 
    return "$difference $periods[$j] {$tense}";
}
 
$date = "2009-03-04 17:45";
$result = Agotime($date); // 2 days ago
 
?>

add Agotime(); function in page any there or any include page, and just user

echo Agotime($data['recordDate']); //// and it display ago time itself

Leave comments!!

Share/Save/Bookmark

7 Responses to “How to create time difference like 1 day ago in php??”

  1. rv says:

    I think there is a bug, if you pass a time minute before it shows 5 hours ago

  2. SAN says:

    rv, you must know about php date requiments. this function not shows your pc date untill you set TimeZone

    add this on script
    if you are on USA
    date_default_timezone_set(’America/Los_Angeles’);
    or if you in india
    date_default_timezone_set(’Asia/Calcutta’);

    more see this page
    http://www.php.net/manual/en/timezones.php

  3. Bijayani says:

    Hi,

    I happened to see your post find it quite informative. I would like to share a link where a software engineer has shared a tip on “Time Difference problem in PHP and MySQL and its solution”. I am sharing it just for the knowledge purpose.

    Here is the link:
    http://www.mindfiresolutions.com/Time-Difference-problem-in-PHP-and-MySQL-and-its-solution-534.php

    Hope you find it useful and of assistance.

    Thanks,
    Bijayani

  4. SAN says:

    Bijayani,
    thanks for sharing this, but you need to put our website url on that page also. so user must know this is based on our post. otherwise we wont allow you to do that. as per our policy.. i guess you understand this

  5. texting girls says:

    How to rotate my blog posts over and over with a preset time interval with PHP code?

  6. David says:

    Rather than doing it in PHP, its better to do it using JavaScript. Check out the following post http://tinywall.info/2011/10/09/facebook-like-friendly-time-with-gmt-and-utc-in-web-application-with-php-javascript/

  7. Neeraj says:

    Thanks for this nice code.

Leave a Reply