PHP function: calculate x seconds, minutes or hours ago

14 February, 2012 by Tom Elliott

Here is a Twitter style PHP time interval calculation function that will return the time difference between two dates in days, hours, minutes or seconds depending on the time interval between the two times.

If the time interval is within 60 seconds, then ‘X seconds ago’ will be displayed. If the interval is between 60 seconds and 60 minutes then ‘X minutes ago’ will appear. Similarly, ‘X hours ago’ will be displayed for a time interval greater than 60 minutes but less than 24 hours and ‘X days ago’ for anything above 24 hours.

The time interval function takes in three arguments:

  1. The old date – the initial datetime in the standard MySQL format i.e. “Y-m-d H:i:s”
  2. The new date – again, in the format “Y-m-d H:i:s”. To use the current ‘now’ time, pass date(“Y-m-d H:i:s”) as the argument
  3. The time interval type. If you want the function to return the most appropriate unit of time automatically, use ‘x’ but this could be overridden by specifying the values for seconds, minutes, hours or days using the values ‘s’, ‘m’, ‘h’ or ‘d’ respectively.

Here is the PHP time interval function:


The following example calls this function, passing 3 arguments for old date (MySQL datetime field from a recordset), new date (current time) and time type (x for automatic).


The resulting time string should appear as ‘X seconds ago’ or ‘X minutes ago’ etc depending on the time interval.



11 Comments

  • CJ says:

    That Script is find just Messed up in a few area’s, Nothing someone who has been using php for more than a day could not find on their own.

    function xTimeAgo ($oldTime, $newTime, $timeType) {
    $timeCalc = strtotime($newTime) – strtotime($oldTime);
    if ($timeType == “x”) {
    if ($timeCalc > 0) {
    $timeType = “s”;
    }
    if ($timeCalc > 60) {
    $timeType = “m”;
    }
    if ($timeCalc > (60*60)) {
    $timeType = “h”;
    }
    if ($timeCalc > (60*60*24)) {
    $timeType = “d”;
    }
    }
    if ($timeType == “s”) {
    $timeCalc .= ” seconds ago”;
    }
    if ($timeType == “m”) {
    $timeCalc = round($timeCalc/60) . ” minutes ago”;
    }
    if ($timeType == “h”) {
    $timeCalc = round($timeCalc/60/60) . ” hours ago”;
    }
    if ($timeType == “d”) {
    $timeCalc = round($timeCalc/60/60/24) . ” days ago”;
    }
    return $timeCalc;
    }

    Replaced the =’s with >’s also included interval for Second’s

  • CJ says:

    Oh yeah here it is Simplified to run a bit quicker with less clutter,

    function xTimeAgo ($oldTime, $newTime) {
    $timeCalc = strtotime($newTime) – strtotime($oldTime);

    if ($timeCalc > (60*60*24)) {$timeCalc = round($timeCalc/60/60/24) . ” days ago”;}
    else if ($timeCalc > (60*60)) {$timeCalc = round($timeCalc/60/60) . ” hours ago”;}
    else if ($timeCalc > 60) {$timeCalc = round($timeCalc/60) . ” minutes ago”;}
    else if ($timeCalc > 0) {$timeCalc .= ” seconds ago”;}

    return $timeCalc;
    }

    this dosent use the “X, D, H, M, S” variables anymore so make sure not to Include them if you use it. 😀

  • Tom Elliott says:

    Hey CJ, thanks for the refinements on the script – the simplified version works well for anyone happy to display seconds, minutes, hours, days ago automatically 🙂

  • harish says:

    Hi Cj, Your code doesnt work for me….

    It always says unepected T_Variable or T_string in the below line
    ———————
    $timeCalc = strtotime($newTime) – strtotime($oldTime);
    ———————

    • Ravi agarwal says:

      Hey, Harish remove – sign as in code and add – sign from your keyboard and also remove “” as in code and add “” from your code. It may be the problem of character set

  • Usama Ahmed says:

    Hi

    I liked your PHP script and ported it to MySQL. Here is the link for it

    http://www.usamaahmed.com/2012/11/mysql-function-to-calculate-x-time-ago.html

    Thanks

  • Jhon Keates says:

    function calculateTimeFromPost($postedDateTime, $systemDateTime, $typeOfTime) {
    $changePostedTimeDate=strtotime($postedDateTime);
    $changeSystemTimeDate=strtotime($systemDateTime);
    $timeCalc=$changeSystemTimeDate-$changePostedTimeDate;
    if ($typeOfTime == “s”) {
    if ($timeCalc > 0) {
    $typeOfTime = “s”;
    }
    if ($timeCalc > 60) {
    $typeOfTime = “m”;
    }
    if ($timeCalc > (60*60)) {
    $typeOfTime = “h”;
    }
    if ($timeCalc > (60*60*24)) {
    $typeOfTime = “d”;
    }
    }
    if ($typeOfTime == “s”) {
    $timeCalc .= ” seconds ago”;
    }
    if ($typeOfTime == “m”) {
    $timeCalc = round($timeCalc/60) . ” minutes ago”;
    }
    if ($typeOfTime == “h”) {
    $timeCalc = round($timeCalc/60/60) . ” hours ago”;
    }
    if ($typeOfTime == “d”) {
    $timeCalc = round($timeCalc/60/60/24) . ” days ago”;
    }
    return $timeCalc;

    }

    /*Function Call*/
    $postedDate=”2012-12-06 15:35:00″;
    /*put echo before calculate*/
    calculateTimeFromPost($postedDate, date(“Y-m-d H:i:s”), “s”)

  • Jhon Keates says:

    Hello Tom

    Thanks for Posting such nice information Have Tested this code but does not work for me have made another function It would be better than the best if you will delete first two post and will approve post dated
    December 6, 2012 at 10:20 am

    Thanks and Have a nice day
    Wish u happy New Year and Happy 12-12-12 in advance because such years come after 800 Years.Chill with guys and gals
    Thanks Tom Keep Posting these Tips and Tricks

  • Justin says:

    Better:

    function xTimeAgo ($oldTime, $newTime) {
    $t = strtotime($newTime) – strtotime($oldTime);
    $multi = “”;
    $_1min = 60;
    $_1hour = 60*60;
    $_1day = 60*60*24;

    if ($t 1) { $multi = “s”; }
    $t = $t . ” second{$multi} ago”;
    } else if ($t >= $_1min && $t ($_1min)) { $multi = “s”; }
    $t = round($t/$_1min) . ” minute{$multi} ago”;
    } else if ($t >= $_1hour && $t ($_1hour*2)) { $multi = “s”; }
    if ($t > ($_1min*2)) { $multi2 = “s”; }
    $hours = floor($t/$_1hour);
    $mins = round($t/$_1min) – ($hours*$_1min);
    $t = “{$hours} hour{$multi}, {$mins} min{$multi2} ago”;
    } else if ($t >= $_1day) {
    if ($t > ($_1day*2)) { $multi = “s”; }
    $t = round($t/$_1day) . ” day{$multi} ago”;
    }
    return $t;
    }

  • Shaig says:

    thank you very muck CJ. it works

  • Shan says:

    It works thanks

  • Leave a Reply

    Your email address will not be published. Required fields are marked *

    css.php