我正在尝试制作一个php代码,检查它是周日上午还是不再是周日上午(09:00到12:00之间)
它应该100%工作,但我看不出我做错了什么
<?php
date_default_timezone_set('Europe/Stockholm'); // timezone
$date = strtotime(time());
if ((date('w', $date) == 0 && date('H', $date) >= 09) ||
(date('w', $date) == 0 && date('H', $date) <= 12)) {
echo "yes, it´s sunday morning";
} else {
echo 'nope, not sunday morning anymore';
}
?>
注:这只是一个特定的星期天,没有确切的日期不确定时区脚本是否是问题
现在它说"不,不是星期天早上",我是把值从12改为18还是不是
试试这个:
<?php
date_default_timezone_set('Europe/Stockholm');
$current_day = date("w");//get the current day : 0 is sunday
$time_now = date("H:i:s");
echo $current_day;
echo($time_now);
if($current_day == 0){//today it is sunday
if($time_now >= "09:00:00" && $time_now <="12:00:00"){
echo "yes, it´s sunday morning";
}
else{
echo "yes, it´s sunday but not morning";
}
}
else{
echo 'nope, not sunday';
}
?>
给定的代码只是检查当天是否为星期日。这将在周日上午进行检查:
<?php
date_default_timezone_set('Europe/Stockholm'); // timezone
$date = strtotime(time());
if ((date('w', $date) == 0 && date('H', $date) >= 09) && date('H', $date) <= 12)) {
echo "yes, it´s sunday morning";
} else {
echo 'nope, not sunday morning anymore';
}
?>
请注意&&
而不是||
尝试:
<?php
date_default_timezone_set('Europe/Stockholm'); // timezone
$date = strtotime(time());
if ( date('w', $date) == 0
&& date('H', $date) >= 09
&& date('H', $date) <= 12 ) {
echo "yes, it´s sunday morning";
} else {
echo 'nope, not sunday morning anymore';
}
?>
你的代码是错误的,星期天的每一次都是在9(date('w',$date)==0之后&;日期('H',$date)>=09)或12之前(日期('w',$date])==0&;date('H',$date)<=12) 。