-6

我在这段代码中有一个错误。请帮我解决它。

function holiday($today) {    

    $year = substr($today, 0, 4);     

    switch($today) {

      case $year.'-01-01':
          $holiday = 'New Year';
          break;

      case $today:
          $today11 = new DateTime($today);
          $R= $today11->format('l') . PHP_EOL;
          $Sunday='0';

          if($R == 0) {
              $holiday = 'Sunday';
          } else {
              $holiday = 'Normal Day';  
          }
      }

      return $holiday;
}

echo $tday= holiday($today); 
4

3 回答 3

0

不确定您为什么要为此使用 switch ,请阅读如何使用switch。下面的功能将起作用:

function holiday($today) {
    // z returns the number of the day in the year, 0 being first of January
    if(date("z", $today) == 0) {
         return "New Year";
    }

    // w returns the number of the day in the week 0-6 where 0 is Sunday
    if(date("w", $today) == 0) {
         return "Sunday";
    }

    return "Normal Day";
}

$today = date();
echo holiday($today);
于 2012-09-06T07:13:19.620 回答
0

这是您的 holiday() 函数的工作实现:

function holiday($today) {    

$date = strtotime($today);     

//check if Sunday
if (date('l', $date) == 'Sunday') {
    return 'Sunday';
}

//check if New Year
if ((date('j', $date) == 1) && (date('n', $date) == 1)) {
    return 'New Year';
}

//else, just return Normal Day
return 'Normal Day';

}

//$today is in YYY/MM/DD format
echo $tday = holiday($today);

此外,在这种情况下,PHP 的date参考可以派上用场:http: //php.net/manual/en/function.date.php

于 2012-09-06T07:16:52.903 回答
0

尝试一下:-

function holyday($today)
{
    $start_date = strtotime($today);


    if(date('z',$start_date) ==0)
    {
         return 'New Year';

    }else{

        if(date('l',$start_date) =='Sunday')
        {
             return 'Sunday';

        }else{

            return "Noraml Day";
        }
    }
}

echo holyday('2012-09-06');

输出 = 正常日

echo holyday('2013-01-01');

产出 = 新年

于 2012-09-06T07:33:31.177 回答