PHP function: calculate x seconds, minutes or hours ago

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 date\time 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:

<?php
function xTimeAgo ($oldTime, $newTime, $timeType) {
        $timeCalc = strtotime($newTime) - strtotime($oldTime);        
        if ($timeType == "x") {
            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;
    }
?>

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).

<?php echo xTimeAgo($row_rsRecordset1['dateField'], date("Y-m-d H:i:s"), "x"); ?>

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

7 Responses to “PHP function: calculate x seconds, minutes or hours ago”

  1. 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

  2. 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. :D

  3. 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 :)

  4. 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);
    ———————

  5. 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

  6. 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”)

  7. 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

Leave a Reply

*