Luk*_*kas 3 php time if-statement range
我必须检查当前的白天是否在特定范围内。我查阅了互联网,发现了类似的几种解决方案:
$now = date("His");//or date("H:i:s")
$start = '130000';//or '13:00:00'
$end = '170000';//or '17:00:00'
if($now >= $start && $now <= $end){
echo "Time in between";
}
else{
echo "Time outside constraints";
}
Run Code Online (Sandbox Code Playgroud)
如果两个条件都必须成立,那么当我们假设$ start为06:00:00且$ end为02:00:00时,如何实现此二。
如果我们假设它是01:00:00,那么在这种情况下第一个条件就不能成立。
有没有人有不同的想法来解决这个问题?
谢谢!
自然,您必须在比较中考虑日期。
<?php
$start = strtotime('2014-11-17 06:00:00');
$end = strtotime('2014-11-18 02:00:00');
if(time() >= $start && time() <= $end) {
// ok
} else {
// not ok
}
Run Code Online (Sandbox Code Playgroud)
如果您需要检查时间范围是否超过午夜
function isWithinTimeRange($start, $end){
$now = date("His");
// time frame rolls over midnight
if($start > $end) {
// if current time is past start time or before end time
if($now >= $start || $now < $end){
return true;
}
}
// else time frame is within same day check if we are between start and end
else if ($now >= $start && $now <= $end) {
return true;
}
return false;
}
Run Code Online (Sandbox Code Playgroud)
然后,您可以通过以下方式获得您是否在该时间范围内
echo isWithinTimeRange(130000, 170000);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7306 次 |
| 最近记录: |