Created
December 8, 2017 13:00
-
-
Save Theodory/0eeea04ad6738287893ac0417180cf38 to your computer and use it in GitHub Desktop.
time_elapsed_string.php
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 | |
$timenow = time(); | |
class LANG { | |
public function sprintf($string) { | |
$arg_list = func_get_args(); | |
$num_args = count($arg_list); | |
for($i = 1; $i < $num_args; $i++) | |
{ | |
$string = str_replace('{'.$i.'}', $arg_list[$i], $string); | |
} | |
return $string; | |
} | |
} | |
$l['rel_justnow'] = "Just now"; | |
$l['rel_ago'] = "ago"; | |
$l['rel_left'] = "left"; | |
$l['rel_less_than'] = "Less than one minute"; | |
$l['rel_second_single'] = "second"; | |
$l['rel_second_plural'] = "seconds"; | |
$l['rel_minute_single'] = "minute"; | |
$l['rel_minute_plural'] = "minutes"; | |
$l['rel_day_single'] = "day"; | |
$l['rel_day_plural'] = "days"; | |
$l['rel_hour_single'] = "hour"; | |
$l['rel_hour_plural'] = "hours"; | |
$l['rel_month_single'] = "month"; | |
$l['rel_month_plural'] = "months"; | |
$l['rel_year_single'] = "year"; | |
$l['rel_year_plural'] = "years"; | |
$l['rel_format'] = "{1} {2}"; | |
function time_elapsed_string($stamp, $full = false) { | |
global $l, $timenow; | |
$diff = ($timenow>$stamp) ? (int)$timenow-$stamp : (int)$stamp-$timenow; | |
$over = ($timenow>$stamp) ? 'ago' : 'left'; | |
$formatter['year'] = 31104000; $how_much['year'] = '100'; | |
$formatter['month'] = 2592000; $how_much['month'] = '12'; | |
$formatter['day'] = 86400; $how_much['day'] = '30'; | |
$formatter['hour'] = 3600; $how_much['hour'] = '24'; | |
$formatter['minute'] = 60; $how_much['minute'] = '60'; | |
$formatter['second'] = 1; $how_much['second'] = '60'; | |
foreach($formatter as $date => $overstamp) { | |
$calc[$date] = floor($diff/$overstamp) % $how_much[$date]; | |
$sp = ($calc[$date] == "1") ? "single" : "plural"; | |
if($calc[$date] == 0) { | |
if($date != 'second') { | |
unset($calc[$date]); | |
} | |
} else { | |
$tostring[$date] = $calc[$date]." ".$l['rel_'.$date.'_'.$sp]; | |
} | |
$reminder = $date; | |
} | |
if(count($calc) == '1' && $reminder = 'seconds') { | |
if($calc['second'] == '0') { | |
$tostring[$date] = $l['rel_justnow']; | |
} else { | |
$tostring[$date] = $l['rel_less_than']; | |
} | |
} else { | |
$tostring[$date] = LANG::sprintf($l['rel_format'], $calc[$date]." ".$l['rel_'.$date.'_'.$sp], $l['rel_'.$over]); | |
} | |
if($full == false) { | |
$display = LANG::sprintf($l['rel_format'], array_shift(array_slice($tostring, 0, 1)), $l['rel_'.$over]); | |
} else { | |
$display = implode(", ", $tostring); | |
} | |
return $display; | |
} | |
$time = "1367267755"; | |
echo time_elapsed_string($time, true).PHP_EOL; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment