note 88464 deleted from function.date by derick
| From: | derick@php.net | Date: | Sun, 22 Mar 2009 18:44:10 +0000 |
| Subject: | note 88464 deleted from function.date by derick | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-152292@lists.php.net to get a copy of this message | ||
Note Submitter: steve at NOSPAMPLEASE dot peartenterprises dot com
----
A simpler duration function: (At least slightly easier to follow)
Also handles plural increments instead of using Day(s) it will figure that out for you.
<?php
function getDuration($s, $l=0){
if($s>86400){ // Day
$tot = $s / 86400;
$r = $s % 86400;
if(floor($tot)>1){$p='s';}else{$p='';}
$t = floor($tot).' Day'.$p;
}else if($s>3600){ // Hour
$tot = $s / 3600;
$r = $s % 3600;
if(floor($tot)>1){$p='s';}else{$p='';}
$t = floor($tot).' Hour'.$p;
}else if($s>60){ // Minute
$tot = $s / 60;
$r = $s % 60;
if(floor($tot)>1){$p='s';}else{$p='';}
$t = floor($tot).' Minute'.$p;
}else{ // Second
if($s>1){$p='s';}else{$p='';}
$t = $s.' Second'.$p;
}
if($r > 0){$t .= getDuration($r,$l+1);}
if($l>0){
return ', '.$t;
}
return $t;
}
print getDuration(93756);
// Outputs: 1 Day, 2 Hours, 2 Minutes, 36 Seconds
?>