如何定义日是星期日和正常日。按日期使用开关盒?

时间:2012-09-06 07:03:50

标签: php javascript

我在此代码中有错误。请帮我解决一下。

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

3 个答案:

答案 0 :(得分:0)

不确定为什么要使用开关,请阅读有关如何使用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);

答案 1 :(得分: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

答案 2 :(得分: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');

输出=新年