简体   繁体   English

PHP检查日期是否在特定时间内

[英]PHP Check if date falls within certain hour

Given a DateTime Object how can I check if the hour falls under a certain range? 给定一个DateTime对象,如何检查小时是否在一定范围内?

$local_time = new DateTime();

//time is after 06:00:00 am
if (date('H', $local_time) >= 6) {

echo "After 6am";

}

DateTime objects are comparible: DateTime对象是可比较的:

$local_time = new DateTime();
$six_am     = new DateTime('6am');

//time is after 06:00:00 am
if ($local_time  > $six_am) {

   echo "After 6am";

}

Demo 演示版

Note: this only works if the comparison is being done on the same day. 注意:仅在同一天进行比较时,此方法才有效。 It's gets more complex if you plan on crossing dates. 如果计划跨日期,它将变得更加复杂。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM