MastodonHelp/web/lib/ght.php
2023-12-26 11:16:24 +01:00

39 lines
1 KiB
PHP

<?php
function ght($ts,$suffa=null,$ddn=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
*/
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.$suffa[0][0] : $out[]=$x.$suffa[0][1];
$ts=$ts-$x*86400;
//ore
$x=floor($ts/3600);
if ($x>0)
($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.$suffa[2][0] : $out[]=$x.$suffa[2][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);
}
?>