这是我的代码。我使用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.";
答案 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
}
为了缩短它,你可以使用tenary operator
printf("Tomorrow is%s saturday", date('w', strtotime("+1 day")) == 6 ? '' : " not");