【问题标题】:How to find Tomorrow day is "saturday" or not in PHP?如何在 PHP 中查找明天是否是“星期六”?
【发布时间】:2016-04-15 13:45:24
【问题描述】:

这是我的代码。我正在使用strtotime 函数查找第二天是星期六。但我的代码结果总是运行else 部分。我哪里错了。

$current_date = date('Y-m-d');

$date = strtotime($current_date); 

$weekDay = date('w', strtotime('+1 day',$date));

if(($weekDay == 'Saturday'))
    echo "Tomorrow is Saturday.";
else
    echo "Tomorrow is not Saturday.";

【问题讨论】:

  • var_dump() 你的变量来真正看到你在处理什么......!此外,strtotime(date('Y-m-d'))time() 相同。
  • w 日期选项返回 0-6,而不是一个单词。

标签: php strtotime


【解决方案1】:

它会给你 6 作为输出。 您可以更改“6”而不是“星期六”。

$date = time(); //Current date 
$weekDay = date('w', strtotime('+1 day',$date));

if(($weekDay == 6)) //Check if the day is saturday or not.
    echo "Tomorrow is saturday.";
else
    echo "Tomorrow is not saturday.";

结果:

明天是星期六。

NOTE :
0 => Sunday
1 => Monday
2 => Tuesday
3 => Wednesday
4 => Thursday
5 => Friday
6 => Saturday

【讨论】:

    【解决方案2】:

    用“l”代替“w”

    $current_date = date('Y-m-d');
    
    $date = strtotime($current_date); 
    
    $weekDay = date('l', strtotime('+1 day',$date));
    if(($weekDay == 'Saturday'))
        echo "Tomorrow is Saturday.";
    else
        echo "Tomorrow is not Saturday.";
    

    【讨论】:

    • 正常工作。谢谢
    【解决方案3】:

    它会给你 6 作为输出。

    $current_date = date('Y-m-d');
    
    $date = strtotime($current_date); 
    
    $weekDay = date('w', strtotime('+1 day',$date));
    
    if(($weekDay == 6))
        echo "Tomorrow is Saturday.";
    else
        echo "Tomorrow is not Saturday.";
    

    【讨论】:

      【解决方案4】:

      在 date() 函数中使用 w 参数有一个非常简单的解决方案

      if (date('w', strtotime("+1 day")) == 6) {
       // tomorrow is saturday
      }
      

      或更客观的解决方案

      if ((new DateTime())->modify('+1 day')->format('w') == 6) {
         // tomorrow is saturday
      }
      
      • w - 返回星期几
      • 6 - 代表星期六

      为了简短起见,您可以使用三元运算符

      printf("Tomorrow is%s saturday", date('w', strtotime("+1 day")) == 6 ? '' : " not");
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-10-26
        • 2018-02-02
        • 1970-01-01
        • 2019-02-10
        • 1970-01-01
        • 1970-01-01
        • 2021-06-17
        相关资源
        最近更新 更多