2023-12-26 11:16:24 +01:00
|
|
|
<?php
|
|
|
|
|
2024-01-02 00:55:18 +01:00
|
|
|
function ght($ts,$fa=null,$sd=2,$bunit='years',$ozero=false) {
|
2023-12-26 11:16:24 +01:00
|
|
|
/*
|
2023-12-28 12:28:18 +01:00
|
|
|
$ts is seconds (can be float)
|
2023-12-29 09:58:08 +01:00
|
|
|
if not null, $fa has to be an array defining the output suffixes, see below its default ;-)
|
2023-12-28 12:28:18 +01:00
|
|
|
$sd is how many decimals to put after a dot after seconds (can be 0)
|
2024-01-02 00:55:18 +01:00
|
|
|
$bunit is "from which unit to start counting"
|
|
|
|
$ozero is whether to include a zero value for a unit
|
2023-12-26 11:16:24 +01:00
|
|
|
*/
|
2024-01-02 00:55:18 +01:00
|
|
|
$bunits=['years', 'weeks', 'days', 'hours', 'minutes', 'seconds'];
|
|
|
|
$cbunits=count($bunits);
|
|
|
|
$obunit=$bunit;
|
|
|
|
$bunit=array_search($bunit,$bunits,true);
|
|
|
|
if ($bunit===false) {
|
|
|
|
fwrite(STDERR,"Function «ght» was called with an unsupported «bunit» value: «{$obunit}».\n");
|
|
|
|
$bunit=0;
|
|
|
|
}
|
|
|
|
$buniti=abs($bunit-$cbunits);
|
|
|
|
if ($fa==null) $fa=['year','years','week','weeks','day','days','hour','hours','minute','minutes','second','seconds'];
|
|
|
|
$sep=', ';
|
2023-12-28 12:28:18 +01:00
|
|
|
$out='';
|
|
|
|
$i=0;
|
2024-01-02 00:55:18 +01:00
|
|
|
$osep='';
|
2023-12-28 12:28:18 +01:00
|
|
|
// years
|
2024-01-02 00:55:18 +01:00
|
|
|
if ($buniti>=6) {
|
|
|
|
$x=floor($ts/31536000);
|
|
|
|
if ($x>0 || $ozero)
|
|
|
|
($x==1) ? $out.=$x.' '.$fa[$i] : $out.=$x.' '.$fa[$i+1];
|
|
|
|
$ts=$ts-$x*31536000;
|
|
|
|
}
|
2023-12-28 12:28:18 +01:00
|
|
|
$i+=2;
|
|
|
|
// weeks
|
2024-01-02 00:55:18 +01:00
|
|
|
if ($buniti>=5) {
|
|
|
|
if ($out!='') $osep=$sep;
|
|
|
|
$x=floor($ts/604800);
|
|
|
|
if ($x>0 || $ozero)
|
|
|
|
($x==1) ? $out.=$osep.$x.' '.$fa[$i] : $out.=$osep.$x.' '.$fa[$i+1];
|
|
|
|
$ts=$ts-$x*604800;
|
|
|
|
}
|
2023-12-28 12:28:18 +01:00
|
|
|
$i+=2;
|
|
|
|
// days
|
2024-01-02 00:55:18 +01:00
|
|
|
if ($buniti>=4) {
|
|
|
|
if ($out!='') $osep=$sep;
|
|
|
|
$x=floor($ts/86400);
|
|
|
|
if ($x>0 || $ozero)
|
|
|
|
($x==1) ? $out.=$osep.$x.' '.$fa[$i] : $out.=$osep.$x.' '.$fa[$i+1];
|
|
|
|
$ts=$ts-$x*86400;
|
|
|
|
}
|
2023-12-28 12:28:18 +01:00
|
|
|
$i+=2;
|
|
|
|
// hours
|
2024-01-02 00:55:18 +01:00
|
|
|
if ($buniti>=3) {
|
|
|
|
if ($out!='') $osep=$sep;
|
|
|
|
$x=floor($ts/3600);
|
|
|
|
if ($x>0 || $ozero)
|
|
|
|
($x==1) ? $out.=$osep.$x.' '.$fa[$i] : $out.=$osep.$x.' '.$fa[$i+1];
|
|
|
|
$ts=$ts-$x*3600;
|
|
|
|
}
|
2023-12-28 12:28:18 +01:00
|
|
|
$i+=2;
|
|
|
|
// minutes
|
2024-01-02 00:55:18 +01:00
|
|
|
if ($buniti>=2) {
|
|
|
|
if ($out!='') $osep=$sep;
|
|
|
|
$x=floor($ts/60);
|
|
|
|
if ($x>0 || $ozero)
|
|
|
|
($x==1) ? $out.=$osep.$x.' '.$fa[$i] : $out.=$osep.$x.' '.$fa[$i+1];
|
|
|
|
$ts=$ts-$x*60;
|
|
|
|
}
|
2023-12-28 12:28:18 +01:00
|
|
|
$i+=2;
|
|
|
|
// seconds
|
2024-01-02 00:55:18 +01:00
|
|
|
if ($buniti>=1) {
|
|
|
|
if ($out!='') $osep=$sep;
|
|
|
|
$x=round($ts,$sd);
|
|
|
|
if ($x>0 || $ozero) {
|
|
|
|
if ($sd>0 || $x!=1)
|
|
|
|
$out.=$osep.$x.' '.$fa[$i+1];
|
|
|
|
elseif ($x==1)
|
|
|
|
$out.=$osep.$x.' '.$fa[$i];
|
|
|
|
}
|
|
|
|
}
|
2023-12-28 12:28:18 +01:00
|
|
|
return $out;
|
2023-12-26 11:16:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
?>
|