Created
July 31, 2013 13:28
-
-
Save odenijs/6121962 to your computer and use it in GitHub Desktop.
Simple timespan calculation
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
/* | |
Input parameter is the UNIX timestamp | |
of the starting date. | |
The second parameter is optional - | |
It's value is the ending date, | |
also UNIX timestamp. If this | |
parameter is not given, the | |
default date is current date. | |
*/ | |
function duration($start,$end=null) { | |
$end = is_null($end) ? time() : $end; | |
$seconds = $end - $start; | |
$days = floor($seconds/60/60/24); | |
$hours = $seconds/60/60%24; | |
$mins = $seconds/60%60; | |
$secs = $seconds%60; | |
$duration=''; | |
if($days>0) $duration .= "$days days "; | |
if($hours>0) $duration .= "$hours hours "; | |
if($mins>0) $duration .= "$mins minutes "; | |
if($secs>0) $duration .= "$secs seconds "; | |
$duration = trim($duration); | |
if($duration==null) $duration = '0 seconds'; | |
return $duration; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment