这个函数让我困惑了一段时间,我只是无法让 foreach 语句工作,这两种情况都有效,因为它会显示单词Today
,Yesterday
但year
, month
, andday
不会出现。
function time_decider($timestamp){
$date = date('m/d/Y', $timestamp);
$dif_date = date('m/d/Y',time() - (24 * 60 * 60));
$a = array( 12 * 30 * 24 * 60 * 60 => 'year',
30 * 24 * 60 * 60 => 'month',
24 * 60 * 60 => 'day',
);
switch ($date) {
case date('m/d/Y'):
return "Today";
break;
case $dif_date:
return "Yesterday";
break;
default:
foreach ($a as $secs => $str) {
$d = $etime / $secs;
if ($d >= 1) {
$r = round($d);
return $r . ' ' . $str . ($r > 1 ? 's ago' : '');
break;
}
}
}
}