PHP 是否支持适当的时间比较 if 语句?
Does PHP support proper time comparisons if-statement?
我想使用 PHP 检查它是否是工作时间。但是我无法使用任何日期函数进行适当的时间比较。 PHP 是否支持使用日期函数进行时间比较?
目前我通过计算一天中经过的分钟数解决了我的问题:
<?php
date_default_timezone_set('Europe/Amsterdam');
$hours = date('G');
$minutes = date('i');
$totalMinutes = ($hours * 60) + $minutes;
$betweenMinutes1 = (8 * 60) + 30;
$betweenMinutes2 = (17 * 60) + 0;
echo $hours.' * 60 + '.$minutes.' = '.$totalMinutes.' minuten van de 1440 op een dag verstreken <br/><br/>';
echo 'if '.$totalMinutes.' is between '.$betweenMinutes1.' and '.$betweenMinutes2.' it is workingtime:';
if (!($totalMinutes > $betweenMinutes1 && $totalMinutes < $betweenMinutes2)) {
echo 'It isn\'t workingtime.';
} else {
echo 'It is workingtime.';
}
?>
这是最好的方法吗,还是有办法在 if 语句中使用适当的日期函数来做到这一点
哇哦,对于一个非常小的条件来说,这是相当长的代码;您只想检查当前时间是否在给定范围内
$current=date("Gi"); // hours without leading zeroes and minutes with.
if($current > 830 && $current < 1700) // 0830 to 1700 hours
{
// we're open
}
回答你的实际问题,是的 PHP 确实有时间比较功能。你可以找到一堆 here
我想使用 PHP 检查它是否是工作时间。但是我无法使用任何日期函数进行适当的时间比较。 PHP 是否支持使用日期函数进行时间比较?
目前我通过计算一天中经过的分钟数解决了我的问题:
<?php
date_default_timezone_set('Europe/Amsterdam');
$hours = date('G');
$minutes = date('i');
$totalMinutes = ($hours * 60) + $minutes;
$betweenMinutes1 = (8 * 60) + 30;
$betweenMinutes2 = (17 * 60) + 0;
echo $hours.' * 60 + '.$minutes.' = '.$totalMinutes.' minuten van de 1440 op een dag verstreken <br/><br/>';
echo 'if '.$totalMinutes.' is between '.$betweenMinutes1.' and '.$betweenMinutes2.' it is workingtime:';
if (!($totalMinutes > $betweenMinutes1 && $totalMinutes < $betweenMinutes2)) {
echo 'It isn\'t workingtime.';
} else {
echo 'It is workingtime.';
}
?>
这是最好的方法吗,还是有办法在 if 语句中使用适当的日期函数来做到这一点
哇哦,对于一个非常小的条件来说,这是相当长的代码;您只想检查当前时间是否在给定范围内
$current=date("Gi"); // hours without leading zeroes and minutes with.
if($current > 830 && $current < 1700) // 0830 to 1700 hours
{
// we're open
}
回答你的实际问题,是的 PHP 确实有时间比较功能。你可以找到一堆 here