Using standard lib ght.php, which is more flexible

This commit is contained in:
pezcurrel 2023-12-28 12:28:18 +01:00
parent 6b705d3b3a
commit 9440903626

View file

@ -1,39 +1,50 @@
<?php
function ght($ts,$suffa=null,$ddn=2) {
function ght($ts,$fa=null,$sd=2) {
/*
* $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
$ts is seconds (can be float)
if not null, $fa has to be an array defining the output suffixes, see below
its default ;-)
$sd is how many decimals to put after a dot after seconds (can be 0)
*/
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
if ($fa==null)
$fa=[' year, ',' years, ',' week, ',' weeks, ',' day, ',' days, ',' hour, ',' hours, ',' minute, ',' minutes, ',' second',' seconds'];
$out='';
$i=0;
// years
$x=floor($ts/31536000);
if ($x>0)
($x==1) ? $out.=$x.$fa[$i] : $out.=$x.$fa[$i+1];
$ts=$ts-$x*31536000;
$i+=2;
// weeks
$x=floor($ts/604800);
if ($x>0)
($x==1) ? $out.=$x.$fa[$i] : $out.=$x.$fa[$i+1];
$ts=$ts-$x*604800;
$i+=2;
// days
$x=floor($ts/86400);
if ($x>0)
($x==1) ? $out[]=$x.$suffa[0][0] : $out[]=$x.$suffa[0][1];
($x==1) ? $out.=$x.$fa[$i] : $out.=$x.$fa[$i+1];
$ts=$ts-$x*86400;
//ore
$i+=2;
// hours
$x=floor($ts/3600);
if ($x>0)
($x==1) ? $out[]=$x.$suffa[1][0] : $out[]=$x.$suffa[1][1];
($x==1) ? $out.=$x.$fa[$i] : $out.=$x.$fa[$i+1];
$ts=$ts-$x*3600;
//minuti
$i+=2;
// minutes
$x=floor($ts/60);
if ($x>0)
($x==1) ? $out[]=$x.$suffa[2][0] : $out[]=$x.$suffa[2][1];
($x==1) ? $out.=$x.$fa[$i] : $out.=$x.$fa[$i+1];
$ts=$ts-$x*60;
//secondi
$x=round($ts,$ddn);
if ($x>0)
($x==1) ? $out[]=$x.$suffa[3][0] : $out[]=$x.$suffa[3][1];
$out=implode(', ',$out);
return($out);
$i+=2;
// seconds
$x=round($ts,$sd);
($x==1) ? $out.=$x.$fa[$i] : $out.=$x.$fa[$i+1];
return $out;
}
?>