Skip to content

Instantly share code, notes, and snippets.

@joshhartman
Created February 20, 2011 02:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joshhartman/835635 to your computer and use it in GitHub Desktop.
Save joshhartman/835635 to your computer and use it in GitHub Desktop.
Days Ago Function
<?php
date_default_timezone_set('America/Chicago'); // Set this in your app's config
function getHowLongAgo($date, $display = array('Year', 'Month', 'Day', 'Hour', 'Minute', 'Second'), $ago = 'Ago')
{
$date = getdate(strtotime($date));
$current = getdate();
$p = array('year', 'mon', 'mday', 'hours', 'minutes', 'seconds');
$factor = array(0, 12, 30, 24, 60, 60);
for ($i = 0; $i < 6; $i++) {
if ($i > 0) {
$current[$p[$i]] += $current[$p[$i - 1]] * $factor[$i];
$date[$p[$i]] += $date[$p[$i - 1]] * $factor[$i];
}
if ($current[$p[$i]] - $date[$p[$i]] > 1) {
$value = $current[$p[$i]] - $date[$p[$i]];
return $value . ' ' . $display[$i] . (($value != 1) ? 's' : '') . ' ' . $ago;
}
}
return '';
}
echo getHowLongAgo('2/19/2011');
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment