i'm looking for a PHP code to change time like this: Fri Jul 16 16:58:46 +0000 2010
to a timestamp like this: 20 secs ago
Any help will be appreciated
i'm looking for a PHP code to change time like this: Fri Jul 16 16:58:46 +0000 2010
to a timestamp like this: 20 secs ago
Any help will be appreciated
Use the strtotime()
function and then compare it to the current time:
$timestamp = strtotime( 'Fri Jul 16 16:58:46 +0000 2010' );
$diff = time() - $timestamp;
This gives you the difference to the current time and you can output it in the way you need.
Try with strtotime like
$my_time = strtotime('Fri Jul 16 16:58:46 +0000 2010');
$diff = time() - $my_time;
echo $diff." Seconds ago..";
you can get the minutes and hours as well like
echo $diff/60."Minutes Ago";
echo $diff/(60*60)."Hours Ago";
Totally you can use
$a = array( 12 * 30 * 24 * 60 * 60 => 'year',
30 * 24 * 60 * 60 => 'month',
24 * 60 * 60 => 'day',
60 * 60 => 'hour',
60 => 'minute',
1 => 'second'
);
foreach ($a as $secs => $str) {
$d = $diff / $secs;
if ($d >= 1) {
$r = round($d);
echo $r . ' ' . $str . ($r > 1 ? 's' : '').' ago..';
}
}
You should use a robust library to handle this, so you can estimate not only seconds but hours, days, weeks, months etc, when the time elapses as long. See i.e http://github.com/jimmiw/php-time-ago