我想知道如何使用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,这里还有一分钟验证...
答案 0 :(得分:3)
您可以获取日期部分的日期/时间和条带。该时间可以与其他时间值进行比较。在下面的代码中,我计算当前时间(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;
}
答案 1 :(得分:0)
$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";
}