我的foreach语句在switch语句中不起作用

时间:2012-11-19 02:52:05

标签: php function foreach switch-statement

这个功能一直困扰着我一段时间,我只是不能让foreach语句工作,两个案例都有效,因为它会显示单词TodayYesterday但是yearmonthday将不会显示。

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;
            }
        }
     }
  }

1 个答案:

答案 0 :(得分:1)

现在,函数$d始终为零,因为$etime未定义