Extended ght() a lot
This commit is contained in:
parent
2c132a5155
commit
13765fb17d
1 changed files with 59 additions and 25 deletions
|
@ -1,48 +1,82 @@
|
|||
<?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)
|
||||
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)
|
||||
$bunit is "from which unit to start counting"
|
||||
$ozero is whether to include a zero value for a unit
|
||||
*/
|
||||
if ($fa==null)
|
||||
$fa=[' year, ',' years, ',' week, ',' weeks, ',' day, ',' days, ',' hour, ',' hours, ',' minute, ',' minutes, ',' second',' seconds'];
|
||||
$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=', ';
|
||||
$out='';
|
||||
$i=0;
|
||||
$osep='';
|
||||
// years
|
||||
$x=floor($ts/31536000);
|
||||
if ($x>0)
|
||||
($x==1) ? $out.=$x.$fa[$i] : $out.=$x.$fa[$i+1];
|
||||
$ts=$ts-$x*31536000;
|
||||
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;
|
||||
}
|
||||
$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;
|
||||
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;
|
||||
}
|
||||
$i+=2;
|
||||
// days
|
||||
$x=floor($ts/86400);
|
||||
if ($x>0)
|
||||
($x==1) ? $out.=$x.$fa[$i] : $out.=$x.$fa[$i+1];
|
||||
$ts=$ts-$x*86400;
|
||||
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;
|
||||
}
|
||||
$i+=2;
|
||||
// hours
|
||||
$x=floor($ts/3600);
|
||||
if ($x>0)
|
||||
($x==1) ? $out.=$x.$fa[$i] : $out.=$x.$fa[$i+1];
|
||||
$ts=$ts-$x*3600;
|
||||
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;
|
||||
}
|
||||
$i+=2;
|
||||
// minutes
|
||||
$x=floor($ts/60);
|
||||
if ($x>0)
|
||||
($x==1) ? $out.=$x.$fa[$i] : $out.=$x.$fa[$i+1];
|
||||
$ts=$ts-$x*60;
|
||||
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;
|
||||
}
|
||||
$i+=2;
|
||||
// seconds
|
||||
$x=round($ts,$sd);
|
||||
($x==1) ? $out.=$x.$fa[$i] : $out.=$x.$fa[$i+1];
|
||||
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];
|
||||
}
|
||||
}
|
||||
return $out;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue