Skip to content

Instantly share code, notes, and snippets.

@jamsesso
Created May 30, 2011 21:30
Show Gist options
  • Save jamsesso/999504 to your computer and use it in GitHub Desktop.
Save jamsesso/999504 to your computer and use it in GitHub Desktop.
Supply a time stamp, get a time ago statement.
<?php
function str_time($stamp)
{
$difference = time() - $stamp;
$intervals = array(
'minute' => 60,
'hour' => 3600,
'day' => 86400,
'month' => 2628000,
'year' => 31536000
);
$time_ago = 'Unknown time ago.';
if($difference >= $intervals['year'])
$time_ago = ((string) round($difference / $intervals['year'])).' years ago.';
elseif($difference >= $intervals['month'])
$time_ago = ((string) round($difference / $intervals['month'])).' months ago.';
elseif($difference >= $intervals['day'])
$time_ago = ((string) round($difference / $intervals['day'])).' days ago.';
elseif($difference >= $intervals['hour'])
$time_ago = ((string) round($difference / $intervals['hour'])).' hours ago.';
elseif($difference >= $intervals['minute'])
$time_ago = ((string) round($difference / $intervals['minute'])).' minutes ago.';
elseif($difference < $intervals['minute'])
$time_ago = ((string) $difference).' seconds ago.';
return $time_ago;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment