php method to query the time range: 1. Create a PHP sample file; 2. Use the "function checkIsBetweenTime($start,$end){...}" method to determine whether it is within the specified time range. Can.
The operating environment of this article: windows7 system, PHP7.1 version, DELL G3 computer
How to query the time range in php?
php Determine whether it is within the specified time period
/** * 判断当前的时分是否在指定的时间段内 * @param $start 开始时分 eg:10:30 * @param $end 结束时分 eg:15:30 * @author:mzc * @date:2018/8/9 10:46 * @return: bool 1:在范围内,0:没在范围内 */ function checkIsBetweenTime($start,$end){ $date= date('H:i'); $curTime = strtotime($date);//当前时分 $assignTime1 = strtotime($start);//获得指定分钟时间戳,00:00 $assignTime2 = strtotime($end);//获得指定分钟时间戳,01:00 $result = 0; if($curTime>$assignTime1&&$curTime<$assignTime2){ $result = 1; } return $result; } $assginTime1 = '00:00'; $assginTime2 = '01:00'; $isBetweenTime = checkIsBetweenTime($assginTime1,$assginTime2);
1. Judgment including hours and minutes
//设置【日期、时间】默认时区 date_default_timezone_set("Asia/Shanghai"); $time = intval (date("Hi")); if ($time > "800" && $time < "1130") { // code }
2. Judgment only Hours
date_default_timezone_set("Asia/Shanghai"); if(date('G')<8 || date('G')>17){ // code }$h = intval(date("H")); if($h > 23 || $h < 7){ echo '这里是第一个任务'; } else { echo '这里是第二个任务'; }
3. PHP judge statements by time period
<?php date_default_timezone_set('PRC');//设置时区,其中PRC为“中华人民共和国” $j=date("H:i");获得当前小时和分钟的时间 $h=strtotime($j);//获得当前小时和分钟的时间时间戳 $z=strtotime('00:00');//获得指定分钟时间戳,00:00 $x=strtotime('00:29');//获得指定分钟时间戳,00:29 if($h>$z && $z<$x){ echo '显示时间是00:00到00:29分之间'; } ?> //(亚洲/重庆) date_default_timezone_set('Asia/Chongqing'); //(亚洲/哈尔滨) date_default_timezone_set('Asia/Harbin'); //(中华人民共和国) date_default_timezone_set('PRC');
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of How to query the time range in php. For more information, please follow other related articles on the PHP Chinese website!