如何定义星期日和平日.按日期使用开关


How to define day is sunday and Normal day. using switch case by date?

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

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

试试看:-

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

输出=Noraml日

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

输出=新年

下面是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

不确定为什么要使用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);