Made it cope with time values < 1 second; changed the way of passing suffixes; other minore changes
This commit is contained in:
parent
d59eb396eb
commit
6ff4752dd7
1 changed files with 15 additions and 15 deletions
|
@ -1,37 +1,37 @@
|
|||
<?php
|
||||
|
||||
function ght($ts,$fa=null,$sd=2) {
|
||||
function ght($ts,$suffa=null,$ddn=2) {
|
||||
/*
|
||||
* $ts è una quantità di secondi (può essere float)
|
||||
* $fa è il formato, tipo così:
|
||||
* $fa=array(' giorno, § giorni, ',' ora, § ore, ',' minuto, § minuti, ',' secondo§ secondi');
|
||||
* $sd è il numero di decimali a cui si vuole arrotondare
|
||||
* $ts is an amount of seconds (can be float)
|
||||
* $suffa is an array defining the suffixes, singular and plural, for each
|
||||
* unit, like this: [[' day',' days'],[' hour',' hours'],[' minute',' minutes'],[' second',' seconds']]
|
||||
* $ddn is the number of decimal digits to round seconds to
|
||||
*/
|
||||
if ($fa==null)
|
||||
// $fa=array('g, §g, ','o, §o, ','m, §m, ','s§s');
|
||||
$fa=array('d§d','h§h','m§m','s§s');
|
||||
foreach ($fa as $k=>$v)
|
||||
$fa[$k]=explode('§',$v);
|
||||
if ($suffa==null)
|
||||
// $suffa=['g','g'],['o','o'],['m','m'],['s','s']];
|
||||
$suffa=[['d','d'],['h','h'],['m','m'],['s','s']];
|
||||
if ($ts<1)
|
||||
return(round($ts,$ddn).$suffa[3][1]);
|
||||
$out=[];
|
||||
//giorni
|
||||
$x=floor($ts/86400);
|
||||
if ($x>0)
|
||||
($x==1) ? $out[]=$x.$fa[0][0] : $out[]=$x.$fa[0][1];
|
||||
($x==1) ? $out[]=$x.$suffa[0][0] : $out[]=$x.$suffa[0][1];
|
||||
$ts=$ts-$x*86400;
|
||||
//ore
|
||||
$x=floor($ts/3600);
|
||||
if ($x>0)
|
||||
($x==1) ? $out[]=$x.$fa[1][0] : $out[]=$x.$fa[1][1];
|
||||
($x==1) ? $out[]=$x.$suffa[1][0] : $out[]=$x.$suffa[1][1];
|
||||
$ts=$ts-$x*3600;
|
||||
//minuti
|
||||
$x=floor($ts/60);
|
||||
if ($x>0)
|
||||
($x==1) ? $out[]=$x.$fa[2][0] : $out[]=$x.$fa[2][1];
|
||||
($x==1) ? $out[]=$x.$suffa[2][0] : $out[]=$x.$suffa[2][1];
|
||||
$ts=$ts-$x*60;
|
||||
//secondi
|
||||
$x=round($ts,$sd);
|
||||
$x=round($ts,$ddn);
|
||||
if ($x>0)
|
||||
($x==1) ? $out[]=$x.$fa[3][0] : $out[]=$x.$fa[3][1];
|
||||
($x==1) ? $out[]=$x.$suffa[3][0] : $out[]=$x.$suffa[3][1];
|
||||
$out=implode(', ',$out);
|
||||
return($out);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue