Home  >  Article  >  Backend Development  >  How to query the time range in php

How to query the time range in php

藏色散人
藏色散人Original
2022-01-11 09:34:462905browse

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.

How to query the time range in php

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 = &#39;00:00&#39;;
 $assginTime2 = &#39;01:00&#39;;
 $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(&#39;G&#39;)<8 || date(&#39;G&#39;)>17){
  // code
}$h = intval(date("H")); 
if($h > 23 || $h < 7){
 echo &#39;这里是第一个任务&#39;;
} else {
 echo &#39;这里是第二个任务&#39;;
}

3. PHP judge statements by time period

<?php 
date_default_timezone_set(&#39;PRC&#39;);//设置时区,其中PRC为“中华人民共和国”
$j=date("H:i");获得当前小时和分钟的时间
$h=strtotime($j);//获得当前小时和分钟的时间时间戳
$z=strtotime(&#39;00:00&#39;);//获得指定分钟时间戳,00:00
$x=strtotime(&#39;00:29&#39;);//获得指定分钟时间戳,00:29
if($h>$z && $z<$x){
  echo &#39;显示时间是00:00到00:29分之间&#39;;
}
?>
//(亚洲/重庆)
date_default_timezone_set(&#39;Asia/Chongqing&#39;);
//(亚洲/哈尔滨)
date_default_timezone_set(&#39;Asia/Harbin&#39;);
//(中华人民共和国)
date_default_timezone_set(&#39;PRC&#39;);

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn