Php 如果时间大于或小于

Php 如果时间大于或小于,php,time,timestamp,minute,Php,Time,Timestamp,Minute,我想知道如何使用php询问时间是否大于 08:30 低于 21:15 如果我使用: <?php if ( (date("H")>8&&date("i")>30) && (date("H")<21&&date("i")<25) ) { echo 'This website is closed from 08:30 - 21:25. Sorry for the Inconvenience.'; exit; } ?&g

我想知道如何使用php询问时间是否大于

08:30

低于

21:15

如果我使用:

<?php

if ( (date("H")>8&&date("i")>30) && (date("H")<21&&date("i")<25) ) {
echo 'This website is closed from 08:30 - 21:25. Sorry for the Inconvenience.';
exit;

}

?>


这不是一种准确的说法,因为即使时间大于8且小于21,这里也有一分钟的验证…

您可以获得日期/时间和日期部分的条带。该时间可以与其他时间值进行比较。在下面的代码中,我计算当前时间8:30和21:15从午夜开始的秒数,然后
if
变得非常简单:

define('SecondsPerDay', 86400);
define('SecondsPerHour', 3600);
define('SecondsPerMinute', 60);

$time = time () % SecondsPerDay;

$from = 8 * SecondsPerHour + 30 * SecondsPerMinute;
$to = 21 * SecondsPerHour + 15 * SecondsPerMinute;

if ($time >= $from && $time < $to) {
  echo 'This website is closed from 08:30 - 21:25. Sorry for the Inconvenience.';
  exit;
}
define('SecondsPerDay',86400);
定义('SecondsPerHour',3600);
定义('SecondsPerMinute',60);
$time=time()%SecondsPerDay;
$from=8*SecondsPerHour+30*SecondsPerMinute;
$to=21*SecondsPerHour+15*SecondsPerMinute;
如果($time>=$from&&$time<$to){
echo“本网站于08:30-21:25关闭,不便之处,敬请原谅。”;
出口
}
$startTime='08:30';
$endTime='21:15';
$time=新日期时间($startTime);
$time1=日期\格式($time,'H:i');
$time=新日期时间($endTime);
$time2=日期\格式($time,'H:i');
“今天是”$当前=日期(“H:i”)。“
”; 如果($current>$time1&&$current<$time2) { echo“本网站于08:30-21:25关闭,不便之处,敬请原谅。”; }否则{ 呼应“它是开放的”; }

如果((日期('Hi')>830)和((日期('Hi')<2125))
@Mark Baker:)非常简单,为什么不简化为
!你能解释一下这些数字吗?21*3600? 15*60? 为什么是3600?为什么是60?看啊看啊看啊,我看不出这一切都是以秒为单位的,所以每天86400秒,每小时3600秒,每分钟60秒。所有的计算都只涉及1天,所以使用模运算将时间保持在24小时范围内,并通过每小时8*秒+每分钟30*秒将8:30转换为秒。我会更新密码,这很聪明。我喜欢这样
$startTime = '08:30';
$endTime = '21:15';
$time = new DateTime($startTime);
$time1 =  date_format($time, 'H:i'); 
$time = new DateTime($endTime);
$time2 =  date_format($time, 'H:i');
"Today is " . $current =date("H:i") . "<br>";
if ($current > $time1 && $current < $time2)
{
echo 'This website is closed from 08:30 - 21:25. Sorry for the Inconvenience.';
}else{

echo "It's open";
}