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 <?php
function ght($ts,$suffa=null,$ddn=2) { function ght($ts,$fa=null,$sd=2) {
/* /*
* $ts is an amount of seconds (can be float) $ts is seconds (can be float)
* $suffa is an array defining the suffixes, singular and plural, for each if not null, $fa has to be an array defining the output suffixes, see below
* unit, like this: [[' day',' days'],[' hour',' hours'],[' minute',' minutes'],[' second',' seconds']] its default ;-)
* $ddn is the number of decimal digits to round seconds to $sd is how many decimals to put after a dot after seconds (can be 0)
*/ */
if ($suffa==null) if ($fa==null)
// $suffa=['g','g'],['o','o'],['m','m'],['s','s']]; $fa=[' year, ',' years, ',' week, ',' weeks, ',' day, ',' days, ',' hour, ',' hours, ',' minute, ',' minutes, ',' second',' seconds'];
$suffa=[['d','d'],['h','h'],['m','m'],['s','s']]; $out='';
if ($ts<1) $i=0;
return(round($ts,$ddn).$suffa[3][1]); // years
$out=[]; $x=floor($ts/31536000);
//giorni 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); $x=floor($ts/86400);
if ($x>0) 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; $ts=$ts-$x*86400;
//ore $i+=2;
// hours
$x=floor($ts/3600); $x=floor($ts/3600);
if ($x>0) 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; $ts=$ts-$x*3600;
//minuti $i+=2;
// minutes
$x=floor($ts/60); $x=floor($ts/60);
if ($x>0) 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; $ts=$ts-$x*60;
//secondi $i+=2;
$x=round($ts,$ddn); // seconds
if ($x>0) $x=round($ts,$sd);
($x==1) ? $out[]=$x.$suffa[3][0] : $out[]=$x.$suffa[3][1]; ($x==1) ? $out.=$x.$fa[$i] : $out.=$x.$fa[$i+1];
$out=implode(', ',$out); return $out;
return($out);
} }
?> ?>