Skip to content

Instantly share code, notes, and snippets.

@svecon
Created March 18, 2015 13:22
Show Gist options
  • Save svecon/459279e784c61d8f1ceb to your computer and use it in GitHub Desktop.
Save svecon/459279e784c61d8f1ceb to your computer and use it in GitHub Desktop.
/**
* Breaksdown a timestamp into an array of days, months, etc since the current time
* @author http://milesj.me/snippets/php/timeBreakdown
* @param int $timeStamp
* @return array
*/
function timeBreakdown($timeStamp) {
if (!is_int($timeStamp)) $timeStamp = strtotime($timeStamp);
$currentTime = time();
$periods = array(
'years' => 31556926,
'months' => 2629743,
'weeks' => 604800,
'days' => 86400,
'hours' => 3600,
'minutes' => 60,
'seconds' => 1
);
$durations = array(
'years' => 0,
'months' => 0,
'weeks' => 0,
'days' => 0,
'hours' => 0,
'minutes' => 0,
'seconds' => 0
);
if ($timeStamp) {
$seconds = $currentTime - $timeStamp;
if ($seconds <= 0){
return $durations;
}
foreach ($periods as $period => $seconds_in_period) {
if ($seconds >= $seconds_in_period) {
$durations[$period] = floor($seconds / $seconds_in_period);
$seconds -= $durations[$period] * $seconds_in_period;
}
}
}
return $durations;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment