Skip to main content

PHP How to find the time elapsed since a date time?



How to find the time elapsed since a date time stamp like 2010-04-28 17:25:43 , final out put text should be like xx Minutes Ago / xx Days Ago





Source: Tips4all

Comments

  1. Most of the answers seem focused around converting the date from a string to time. It seems you're mostly thinking about getting the date into the '5 days ago' format, etc.. right?

    This is how I'd go about doing that:

    $time = strtotime('2010-04-28 17:25:43');

    echo 'event happened '.humanTiming($time).' ago';

    function humanTiming ($time)
    {

    $time = time() - $time; // to get the time since that moment

    $tokens = array (
    31536000 => 'year',
    2592000 => 'month',
    604800 => 'week',
    86400 => 'day',
    3600 => 'hour',
    60 => 'minute',
    1 => 'second'
    );

    foreach ($tokens as $unit => $text) {
    if ($time < $unit) continue;
    $numberOfUnits = floor($time / $unit);
    return $numberOfUnits.' '.$text.(($numberOfUnits>1)?'s':'');
    }

    }


    I haven't tested that, but it should work.

    The result would look like

    event happened 4 days ago


    or

    event happened 1 minute ago


    cheers

    ReplyDelete
  2. Want to share php function which results in grammetically correct facebook like human reable time format.

    Example:

    echo get_time_ago(strtotime('now'));

    Result:

    less than 1 minute ago

    function get_time_ago($time_stamp)
    {
    $time_difference = strtotime('now') - $time_stamp;

    if ($time_difference >= 60 * 60 * 24 * 365.242199)
    {
    /*
    * 60 seconds/minute * 60 minutes/hour * 24 hours/day * 365.242199 days/year
    * This means that the time difference is 1 year or more
    */
    return get_time_ago_string($time_stamp, 60 * 60 * 24 * 365.242199, 'year');
    }
    elseif ($time_difference >= 60 * 60 * 24 * 30.4368499)
    {
    /*
    * 60 seconds/minute * 60 minutes/hour * 24 hours/day * 30.4368499 days/month
    * This means that the time difference is 1 month or more
    */
    return get_time_ago_string($time_stamp, 60 * 60 * 24 * 30.4368499, 'month');
    }
    elseif ($time_difference >= 60 * 60 * 24 * 7)
    {
    /*
    * 60 seconds/minute * 60 minutes/hour * 24 hours/day * 7 days/week
    * This means that the time difference is 1 week or more
    */
    return get_time_ago_string($time_stamp, 60 * 60 * 24 * 7, 'week');
    }
    elseif ($time_difference >= 60 * 60 * 24)
    {
    /*
    * 60 seconds/minute * 60 minutes/hour * 24 hours/day
    * This means that the time difference is 1 day or more
    */
    return get_time_ago_string($time_stamp, 60 * 60 * 24, 'day');
    }
    elseif ($time_difference >= 60 * 60)
    {
    /*
    * 60 seconds/minute * 60 minutes/hour
    * This means that the time difference is 1 hour or more
    */
    return get_time_ago_string($time_stamp, 60 * 60, 'hour');
    }
    else
    {
    /*
    * 60 seconds/minute
    * This means that the time difference is a matter of minutes
    */
    return get_time_ago_string($time_stamp, 60, 'minute');
    }
    }

    function get_time_ago_string($time_stamp, $divisor, $time_unit)
    {
    $time_difference = strtotime("now") - $time_stamp;
    $time_units = floor($time_difference / $divisor);

    settype($time_units, 'string');

    if ($time_units === '0')
    {
    return 'less than 1 ' . $time_unit . ' ago';
    }
    elseif ($time_units === '1')
    {
    return '1 ' . $time_unit . ' ago';
    }
    else
    {
    /*
    * More than "1" $time_unit. This is the "plural" message.
    */
    // TODO: This pluralizes the time unit, which is done by adding "s" at the end; this will not work for i18n!
    return $time_units . ' ' . $time_unit . 's ago';
    }
    }

    ReplyDelete
  3. Wrote my own

    function getElapsedTime($eventTime)
    {
    $totaldelay = time() - strtotime($eventTime);
    if($totaldelay <= 0)
    {
    return '';
    }
    else
    {
    if($days=floor($totaldelay/86400))
    {
    $totaldelay = $totaldelay % 86400;
    return $days.' days ago.';
    }
    if($hours=floor($totaldelay/3600))
    {
    $totaldelay = $totaldelay % 3600;
    return $hours.' hours ago.';
    }
    if($minutes=floor($totaldelay/60))
    {
    $totaldelay = $totaldelay % 60;
    return $minutes.' minutes ago.';
    }
    if($seconds=floor($totaldelay/1))
    {
    $totaldelay = $totaldelay % 1;
    return $seconds.' seconds ago.';
    }
    }
    }

    ReplyDelete
  4. One option that'll work with any version of PHP is to do what's already been suggested, which is something like this:

    $eventTime = '2010-04-28 17:25:43';
    $age = time() - strtotime($eventTime);


    That will give you the age in seconds. From there, you can display it however you wish.

    One problem with this approach, however, is that it won't take into account time shifts causes by DST. If that's not a concern, then go for it. Otherwise, you'll probably want to use the diff() method in the DateTime class. Unfortunately, this is only an option if you're on at least PHP 5.3.

    ReplyDelete
  5. I think I have a function which should do what you want:

    function time2string($timeline) {
    $periods = array('day' => 86400, 'hour' => 3600, 'minute' => 60, 'second' => 1);

    foreach($periods AS $name => $seconds){
    $num = floor($timeline / $seconds);
    $timeline -= ($num * $seconds);
    $ret .= $num.' '.$name.(($num > 1) ? 's' : '').' ';
    }

    return trim($ret);
    }


    Simply apply it to the difference between time() and strtotime('2010-04-28 17:25:43') as so:

    print time2string(time()-strtotime('2010-04-28 17:25:43')).' ago';

    ReplyDelete
  6. Convert [saved_date] to timestamp. Get current timestamp.

    current timestamp - [saved_date] timestamp.

    Then you can format it with date();

    You can normally convert most date formats to timestamps with the strtotime() function.

    ReplyDelete
  7. To find out time elapsed i usually use time() instead of date() and formatted time stamps.
    Then get the difference between the latter value and the earlier value and format accordingly. time() is differently not a replacement for date() but it totally helps when calculating elapsed time.

    example:

    The value of time() looks something like this 1274467343 increments every second. So you could have $erlierTime with value 1274467343 and $latterTime with value 1274467500, then just do $latterTime - $erlierTime to get time elapsed in seconds.

    ReplyDelete

Post a Comment

Popular posts from this blog

[韓日関係] 首相含む大幅な内閣改造の可能性…早ければ来月10日ごろ=韓国

div not scrolling properly with slimScroll plugin

I am using the slimScroll plugin for jQuery by Piotr Rochala Which is a great plugin for nice scrollbars on most browsers but I am stuck because I am using it for a chat box and whenever the user appends new text to the boxit does scroll using the .scrollTop() method however the plugin's scrollbar doesnt scroll with it and when the user wants to look though the chat history it will start scrolling from near the top. I have made a quick demo of my situation http://jsfiddle.net/DY9CT/2/ Does anyone know how to solve this problem?

Why does this javascript based printing cause Safari to refresh the page?

The page I am working on has a javascript function executed to print parts of the page. For some reason, printing in Safari, causes the window to somehow update. I say somehow, because it does not really refresh as in reload the page, but rather it starts the "rendering" of the page from start, i.e. scroll to top, flash animations start from 0, and so forth. The effect is reproduced by this fiddle: http://jsfiddle.net/fYmnB/ Clicking the print button and finishing or cancelling a print in Safari causes the screen to "go white" for a sec, which in my real website manifests itself as something "like" a reload. While running print button with, let's say, Firefox, just opens and closes the print dialogue without affecting the fiddle page in any way. Is there something with my way of calling the browsers print method that causes this, or how can it be explained - and preferably, avoided? P.S.: On my real site the same occurs with Chrome. In the ex