views:

401

answers:

6

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'

A: 

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.

Babiker
A: 

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.

Evernoob
Unfortunately date() does not allow to calculate the number of days /minutes... between two dates...
mexique1
+1  A: 

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.

mr. w
Didn't know DateTime::diff. It rocks ! On the PHP manual there is a retro portage of date_diff => http://us2.php.net/manual/en/datetime.diff.php#97810
mexique1
A: 

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';
JoeR
that would return a string like "3 hours 5 minutes 6 seconds ago", right?
arnorhs
Yep, it would do. Possibly misread the OP's question a bit, in which case it'd need some tweaking for their use.
JoeR
+2  A: 

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

arnorhs
Why create the array from least to most only to call array_reverse? Regardless, I believe you need to pass "preserve_keys" as true as the second parameter to array_reverse.
jeremy
good point about reversing. I haven't tried the code, so I'm guessing you're right about preserve_keys.
arnorhs
A: 

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.';
        }
    }
}
Mithun P
Let me get this straight. You wrote your own ugly-code version that actually works worse than the other suggestions, harder to maintain etc - thereby also disrespecting all the people that took the time to write you an answer.What a douche....
arnorhs
At least my code does not ave any for loops.And there is no act of disrespect as I have followed the way directed by the persons who answered here.
Mithun P