MastodonHelp/web/site/mustard/include/ght.php

40 lines
1 KiB
PHP
Raw Normal View History

2020-10-13 08:21:26 +02:00
<?php
function ght($ts,$suffa=null,$ddn=2) {
2020-10-13 08:21:26 +02:00
/*
* $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
2020-10-13 08:21:26 +02:00
*/
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]);
2022-12-15 23:52:32 +01:00
$out=[];
2020-10-13 08:21:26 +02:00
//giorni
$x=floor($ts/86400);
if ($x>0)
($x==1) ? $out[]=$x.$suffa[0][0] : $out[]=$x.$suffa[0][1];
2020-10-13 08:21:26 +02:00
$ts=$ts-$x*86400;
//ore
$x=floor($ts/3600);
if ($x>0)
($x==1) ? $out[]=$x.$suffa[1][0] : $out[]=$x.$suffa[1][1];
2020-10-13 08:21:26 +02:00
$ts=$ts-$x*3600;
//minuti
$x=floor($ts/60);
if ($x>0)
($x==1) ? $out[]=$x.$suffa[2][0] : $out[]=$x.$suffa[2][1];
2020-10-13 08:21:26 +02:00
$ts=$ts-$x*60;
//secondi
$x=round($ts,$ddn);
2022-12-15 23:52:32 +01:00
if ($x>0)
($x==1) ? $out[]=$x.$suffa[3][0] : $out[]=$x.$suffa[3][1];
2022-12-15 23:52:32 +01:00
$out=implode(', ',$out);
2020-10-13 08:21:26 +02:00
return($out);
}
?>