php判断是不是在某个时间段内

方式一、判断小时和分钟

<?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;
	}
}
?>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值