Extended ght() a lot

This commit is contained in:
pezcurrel 2024-01-02 00:55:18 +01:00
parent 2c132a5155
commit 13765fb17d

View file

@ -1,48 +1,82 @@
<?php <?php
function ght($ts,$fa=null,$sd=2) { function ght($ts,$fa=null,$sd=2,$bunit='years',$ozero=false) {
/* /*
$ts is seconds (can be float) $ts is seconds (can be float)
if not null, $fa has to be an array defining the output suffixes, see below its default ;-) 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) $sd is how many decimals to put after a dot after seconds (can be 0)
$bunit is "from which unit to start counting"
$ozero is whether to include a zero value for a unit
*/ */
if ($fa==null) $bunits=['years', 'weeks', 'days', 'hours', 'minutes', 'seconds'];
$fa=[' year, ',' years, ',' week, ',' weeks, ',' day, ',' days, ',' hour, ',' hours, ',' minute, ',' minutes, ',' second',' 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=', ';
$out=''; $out='';
$i=0; $i=0;
$osep='';
// years // years
$x=floor($ts/31536000); if ($buniti>=6) {
if ($x>0) $x=floor($ts/31536000);
($x==1) ? $out.=$x.$fa[$i] : $out.=$x.$fa[$i+1]; if ($x>0 || $ozero)
$ts=$ts-$x*31536000; ($x==1) ? $out.=$x.' '.$fa[$i] : $out.=$x.' '.$fa[$i+1];
$ts=$ts-$x*31536000;
}
$i+=2; $i+=2;
// weeks // weeks
$x=floor($ts/604800); if ($buniti>=5) {
if ($x>0) if ($out!='') $osep=$sep;
($x==1) ? $out.=$x.$fa[$i] : $out.=$x.$fa[$i+1]; $x=floor($ts/604800);
$ts=$ts-$x*604800; if ($x>0 || $ozero)
($x==1) ? $out.=$osep.$x.' '.$fa[$i] : $out.=$osep.$x.' '.$fa[$i+1];
$ts=$ts-$x*604800;
}
$i+=2; $i+=2;
// days // days
$x=floor($ts/86400); if ($buniti>=4) {
if ($x>0) if ($out!='') $osep=$sep;
($x==1) ? $out.=$x.$fa[$i] : $out.=$x.$fa[$i+1]; $x=floor($ts/86400);
$ts=$ts-$x*86400; if ($x>0 || $ozero)
($x==1) ? $out.=$osep.$x.' '.$fa[$i] : $out.=$osep.$x.' '.$fa[$i+1];
$ts=$ts-$x*86400;
}
$i+=2; $i+=2;
// hours // hours
$x=floor($ts/3600); if ($buniti>=3) {
if ($x>0) if ($out!='') $osep=$sep;
($x==1) ? $out.=$x.$fa[$i] : $out.=$x.$fa[$i+1]; $x=floor($ts/3600);
$ts=$ts-$x*3600; if ($x>0 || $ozero)
($x==1) ? $out.=$osep.$x.' '.$fa[$i] : $out.=$osep.$x.' '.$fa[$i+1];
$ts=$ts-$x*3600;
}
$i+=2; $i+=2;
// minutes // minutes
$x=floor($ts/60); if ($buniti>=2) {
if ($x>0) if ($out!='') $osep=$sep;
($x==1) ? $out.=$x.$fa[$i] : $out.=$x.$fa[$i+1]; $x=floor($ts/60);
$ts=$ts-$x*60; if ($x>0 || $ozero)
($x==1) ? $out.=$osep.$x.' '.$fa[$i] : $out.=$osep.$x.' '.$fa[$i+1];
$ts=$ts-$x*60;
}
$i+=2; $i+=2;
// seconds // seconds
$x=round($ts,$sd); if ($buniti>=1) {
($x==1) ? $out.=$x.$fa[$i] : $out.=$x.$fa[$i+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];
}
}
return $out; return $out;
} }