如何找到明天是"星期六"或不在PHP?

时间:2016-04-15 13:45:25

标签: php strtotime

这是我的代码。我使用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.";

4 个答案:

答案 0 :(得分:3)

使用'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.";

答案 1 :(得分:3)

它会给你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 :(得分:0)

它会给你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.";

答案 3 :(得分:0)

在date()函数

中使用 w param有一个非常简单的解决方案
if (date('w', strtotime("+1 day")) == 6) {
 // tomorrow is saturday
}

或更客观的解决方案

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

为了缩短它,你可以使用tenary operator

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