Created
February 20, 2011 02:48
-
-
Save joshhartman/835635 to your computer and use it in GitHub Desktop.
Days Ago Function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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