-
-
Save 0xnbk/577001 to your computer and use it in GitHub Desktop.
Timeline function (PHP)
This file contains hidden or 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 | |
function timespan($time1, $time2 = NULL, $output = 'years,months,weeks,days,hours,minutes,seconds') | |
{ | |
$output = preg_split('/[^a-z]+/', strtolower((string) $output)); | |
if (empty($output)) | |
return FALSE; | |
extract(array_flip($output), EXTR_SKIP); | |
$time1 = max(0, (int) $time1); | |
$time2 = empty($time2) ? time() : max(0, (int) $time2); | |
$timespan = abs($time1 - $time2); | |
isset($years) and $timespan -= 31556926 * ($years = (int) floor($timespan / 31556926)); | |
isset($months) and $timespan -= 2629744 * ($months = (int) floor($timespan / 2629743.83)); | |
isset($weeks) and $timespan -= 604800 * ($weeks = (int) floor($timespan / 604800)); | |
isset($days) and $timespan -= 86400 * ($days = (int) floor($timespan / 86400)); | |
isset($hours) and $timespan -= 3600 * ($hours = (int) floor($timespan / 3600)); | |
isset($minutes) and $timespan -= 60 * ($minutes = (int) floor($timespan / 60)); | |
isset($seconds) and $seconds = $timespan; | |
unset($timespan, $time1, $time2); | |
$deny = array_flip(array('deny', 'key', 'difference', 'output')); | |
$difference = array(); | |
foreach ($output as $key) { | |
if (isset($$key) AND ! isset($deny[$key])) { | |
$difference[$key] = $$key; | |
} | |
} | |
if (empty($difference)) | |
return FALSE; | |
if (count($difference) === 1) | |
return current($difference); | |
return $difference; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Timeline function (PHP)
Since Twitter became very popular, I started to hear many people saying that they absolutely love how Twitter display time informations: 1 hour ago, about 7 days ago, less than a minute ago, etc. This is what the function below do.