Skip to content

Instantly share code, notes, and snippets.

@MauMaGau
Created April 4, 2012 13:45
Show Gist options
  • Save MauMaGau/2301137 to your computer and use it in GitHub Desktop.
Save MauMaGau/2301137 to your computer and use it in GitHub Desktop.
PHP: time ago
<?php
/* Time ago */
$timestamp = 1332686482; // a unix timestamp
$t = array('year'=>31556926,'month'=>2629744,'week'=>604800,'day'=>86400,'hour'=>3600,'minute'=>60,'second'=>1);
// Calculate how long ago the timestamp is
$diff = time() - $timestamp;
foreach($t as $unit=>$seconds){
if($seconds<=$diff){
$v=floor($diff/$seconds);
$nice_diff="$v $unit".($v==1?'':'s').' ago';
break;
}
}
echo $nice_diff;
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment