I xdebug tarafından relativeTime işlevini seviyorum. Sorun bazı boyutu var bunu gerekli değildir.
In other words stop at seconds or minutes if I decide.
So now,
echo fTime(strtotime('-23 hours 5 minutes 55 seconds'),0);
gösterecektir,
23 hours, 5 minutes ago
Yerine
23 hours, 5 minutes, 55 seconds ago
I also wanted it to NOT go lower in the array if it reached one of the higher time amounts.
So if it shows years, I only want to show years and months.
So now,
echo fTime(strtotime('-1 year 2 months 3 weeks 4 days 16 hours 15 minutes 22 seconds'),0);
Gösterebilir misiniz
1 year, 2 months ago
Yerine
1 year, 2 months, 3 weeks, 4 days, 16 hours, 15 minutes, 22 seconds ago
The following code change did what I needed. Props go to xdebug first of course.
Hopefully someone else might find it useful:
function fTime($time, $gran=-1) {
$d[0] = array(1,"second");
$d[1] = array(60,"minute");
$d[2] = array(3600,"hour");
$d[3] = array(86400,"day");
$d[4] = array(604800,"week");
$d[5] = array(2592000,"month");
$d[6] = array(31104000,"year");
$w = array();
$return = "";
$now = time();
$diff = ($now-$time);
$secondsLeft = $diff;
$stopat = 0;
for($i=6;$i>$gran;$i--)
{
$w[$i] = intval($secondsLeft/$d[$i][0]);
$secondsLeft -= ($w[$i]*$d[$i][0]);
if($w[$i]!=0)
{
$return.= abs($w[$i]) . " " . $d[$i][1] . (($w[$i]>1)?'s':'') ." ";
switch ($i) {
case 6: // shows years and months
if ($stopat==0) { $stopat=5; }
break;
case 5: // shows months and weeks
if ($stopat==0) { $stopat=4; }
break;
case 4: // shows weeks and days
if ($stopat==0) { $stopat=3; }
break;
case 3: // shows days and hours
if ($stopat==0) { $stopat=2; }
break;
case 2: // shows hours and minutes
if ($stopat==0) { $stopat=1; }
break;
case 1: // shows minutes and seconds if granularity is not set higher
break;
}
if ($i===$stopat) { break 0; }
}
}
$return .= ($diff>0)?"ago":"left";
return $return;
}
Marcus