Last active
October 17, 2015 10:08
-
-
Save bitsmanent/3f281d75b39cead21e3f to your computer and use it in GitHub Desktop.
Timestamp to Humans Time
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
global $HT_DEFAULT_FMTS; | |
$HT_DEFAULT_FMTS = array( | |
'year_0' => "An year ago", | |
'years_0' => "%d years ago", | |
'year_1' => "An year from now", | |
'years_1' => "%d years from now", | |
'month_0' => "A month ago", | |
'months_0' => "%d months ago", | |
'month_1' => "A month from now", | |
'months_1' => "%d months from now", | |
'day_0' => "A day ago", | |
'days_0' => "%d days ago", | |
'day_1' => "A day from now", | |
'days_1' => "%d days from now", | |
'hour_0' => "An hour ago", | |
'hours_0' => "%d hours ago", | |
'hour_1' => "An hour from now", | |
'hours_1' => "%d hours from now", | |
'minute_0' => "A minute ago", | |
'minutes_0' => "%d minutes ago", | |
'minute_1' => "A minute from now", | |
'minutes_1' => "%d minutes from now", | |
'second_0' => "A second ago", | |
'seconds_0' => "%d seconds ago", | |
'second_1' => "A second from now", | |
'seconds_1' => "%d seconds from now", | |
); | |
global $HT_DIVS; | |
$HT_DIVS = array( | |
'year' => 60*60*24*365, | |
'month' => 60*60*24*31, | |
'day' => 60*60*24, | |
'hour' => 60*60, | |
'minute' => 60, | |
'second' => 0 | |
); | |
function humanstime($timestamp, $texts = null) { | |
global $HT_DIVS; | |
$divs = $HT_DIVS; | |
$diff = time() - $timestamp; | |
$isfuture = ($diff < 0); | |
if($isfuture) | |
$diff = -$diff; | |
foreach($divs as $name => $delta) { | |
if($diff >= $delta) | |
break; | |
} | |
$unit = $delta ? $diff / $delta : $diff; | |
$ht = array( | |
'name' => $name.($unit > 1 ? 's' : ''), | |
'unit' => $unit, | |
'isfuture' => $isfuture | |
); | |
if(!$texts) { | |
global $HT_DEFAULT_FMTS; | |
$fmts = $HT_DEFAULT_FMTS; | |
} | |
$k = $ht['name'].'_'.(int)$ht['isfuture']; | |
return sprintf($fmts[$k], $ht['unit']); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment