0

This question is based on another StackOverFlow question, which is entitled Converting timestamp to time ago in PHP e.g 1 day ago, 2 days ago…

The ticked answer was unexplained but showed this function:

function time_elapsed_string($ptime)
{
    $etime = time() - $ptime;

    if ($etime < 1)
    {
        return '0 seconds';
    }

    $a = array( 365 * 24 * 60 * 60  =>  'year',
                 30 * 24 * 60 * 60  =>  'month',
                      24 * 60 * 60  =>  'day',
                           60 * 60  =>  'hour',
                                60  =>  'minute',
                                 1  =>  'second'
                );
    $a_plural = array( 'year'   => 'years',
                       'month'  => 'months',
                       'day'    => 'days',
                       'hour'   => 'hours',
                       'minute' => 'minutes',
                       'second' => 'seconds'
                );

    foreach ($a as $secs => $str)
    {
        $d = $etime / $secs;
        if ($d >= 1)
        {
            $r = round($d);
            return $r . ' ' . ($r > 1 ? $a_plural[$str] : $str) . ' ago';
        }
    }
}

The time format I have is YYYY-MM-DD HH-MM-SS, for example: 2015-01-21 19:23:09.

My attempt:

echo $target_time = time_elapsed_string(strtotime("2015-01-21 19:23:09"));

It gives:

45 years ago.

Any time is giving also 45 years old. I don't know how the function works, but any help is appreciated.

Community
  • 1
  • 1
George Chalhoub
  • 14,968
  • 3
  • 38
  • 61
  • What is the return value from `strtotime`? Have you tried testing your `time_elapsed_string` function with known test values? – Dai Jan 21 '15 at 18:37
  • strtotime gives 1421886189 – George Chalhoub Jan 21 '15 at 18:42
  • Here your code works fine, have you checked the date of your server? – acontell Jan 21 '15 at 18:45
  • https://github.com/briannesbitt/Carbon, specifically the `diffForHumans` function. – ceejayoz Jan 21 '15 at 18:48
  • possible duplicate of [Converting timestamp to time ago in PHP e.g 1 day ago, 2 days ago...](http://stackoverflow.com/questions/1416697/converting-timestamp-to-time-ago-in-php-e-g-1-day-ago-2-days-ago) -> see the 2nd answer. – Glavić Jan 21 '15 at 21:00

2 Answers2

2

This can easily be accomplished using PHP's DateTime and DateDiff objects

$datetime1 = new DateTime();
$datetime2 = new DateTime("2015-01-21 19:23:09");
$interval = $datetime1->diff($datetime2);
echo $interval->format('%R%a days');

For a full description of the format string to pass, check out the documentation.

Robbert
  • 6,481
  • 5
  • 35
  • 61
0

I found an easy, and efficient solution from http://timeago.yarp.com:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script src="http://timeago.yarp.com/jquery.timeago.js"></script>
<script>
jQuery(document).ready(function() {
  jQuery("abbr.timeago").timeago();
});
</script>

<abbr class="timeago" title="2015-01-21T21:30:00"></abbr>
George Chalhoub
  • 14,968
  • 3
  • 38
  • 61