如果时间大于和小于
If time is bigger than and lower than
我想知道如何使用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,这里也会进行分钟验证...
您可以获得 date/time 和日期部分的剥离。该时间可以与其他时间值进行比较。在下面的代码中,我计算了当前时间从午夜开始的秒数 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;
}
$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";
}
我想知道如何使用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,这里也会进行分钟验证...
您可以获得 date/time 和日期部分的剥离。该时间可以与其他时间值进行比较。在下面的代码中,我计算了当前时间从午夜开始的秒数 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;
}
$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";
}