note 88464 modified in function.date by danbrown
| From: | danbrown@php.net | Date: | Tue, 27 Jan 2009 02:40:49 +0000 |
| Subject: | note 88464 modified in function.date by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-149824@lists.php.net to get a copy of this message | ||
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
?>
--was--
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.
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
http://php.net/manual/en/function.date.php