note 83246 deleted from function.time by aidan
| From: | aidan@php.net | Date: | Wed, 15 Apr 2009 16:05:33 +0000 |
| Subject: | note 83246 deleted from function.time by aidan | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-153333@lists.php.net to get a copy of this message | ||
Note Submitter: south dot minds at gmail dot com
----
Format seconds (if less or equal to 86400) into hours, minutes and seconds ± 1 sec. Change $time
and $myLocale to whatever your want and test from CLI (bash - linux) with
t=''; for i in $(seq 1 20); do t=$t"\n"$(php -f seconds2time.php) ; done ; echo
-e "$t" | sort -n
<?php
$time = mt_rand(0,86400);
$myLocale = "es_AR";
setlocale(LC_ALL,$myLocale);
$localeconv = localeconv();
$dec_point =$localeconv['decimal_point'];
($time > 86400) ? die("Time must be less than 86400 seconds") : '';
if($time % 3600 == 0){
$t_h[0] = $time/3600;
$t_m[0] = 0;
$t_s[0] = 0;
}else if ($time >= 3600 && ($time % 60) == 0 ){
$t_h = explode($dec_point,$time / 3600);
$t_m = explode($dec_point,('0.'.$t_h[1]) * 3600 / 60);
$t_s[0] = 0;
}else if($time < 3600 && ($time % 60) == 0 ){
$t_h[0] = 0;
$t_m = explode($dec_point,$time/ 60);
$t_s[0] = 0;
}else{
$t_h = explode($dec_point,$time / 3600);
$t_m = explode($dec_point,('0.'.$t_h[1]) * 3600 / 60);
$t_s[0] = ceil(('0.'.$t_m[1]) * 60);
}
print sprintf('%05d',$time) .' = ' . sprintf('%02d',$t_h[0]). '
hs. '. sprintf('%02d',$t_m[0]) . ' min. '
.sprintf('%02d',$t_s[0]) ." seg. \n";
?>