方式一、判断小时和分钟
<?php
date_default_timezone_set("Asia/Shanghai");//设置时区,亚洲/上海时区
$time = intval (date("Hi"));
if ($time > "800" && $time < "1130") {
echo "在指定的时间段内";
}
?>
方式二、只判断小时
<?php
date_default_timezone_set("Asia/Shanghai");//设置时区,亚洲/上海时区
$h = intval(date("H"));
if ($h > 24 || $h < 6) {
echo '在凌晨时间段内';
}
?>
方式三、指定的时间段内
<?php
date_default_timezone_set('PRC');//设置时区,其中PRC为“中华人民共和国”
$currentTime=strtotime(date("H:i"));//获得当前小时和分钟的时间时间戳
$startTime=strtotime('10:50');//获得开始时间的时间戳
$endTime=strtotime('10:59');//获得结束时间的时间戳
if($currentTime>=$startTime && $currentTime<=$endTime){
echo '在指定时间段内';
}else{
echo '不在指定时间段内';
}
?>
具体的使用示例:
<?php
date_default_timezone_set('PRC');
header('Content-Type: text/html;charset=utf-8');
$code="fc3d";
$arr=array('fc3d','ssq','qlc','dlt','pl3','pl5','qxc','hk6','amlhc');
if(in_array($code,$arr)){
//获得当前小时和分钟的时间时间戳
$currentTime=strtotime(date("H:i"));
//获得开始时间的时间戳
$startTime=strtotime('20:25');
//获得结束时间的时间戳
$endTime=strtotime('22:00');
if($currentTime<$startTime || $currentTime>$endTime){
echo '['.$code.'] 不在指定的采集时间段内(20:25 ~ 22:00)';
exit;
}
}
?>