简体   繁体   English

PHP-检查给定日期是否适合给定间隔

[英]PHP - Check if given Date fits a given interval

If I have a DateTime interval, such as: 如果我有一个DateTime间隔,例如:

$slot_interval = DateInterval::createFromDatestring('30 minutes');

And another DateTime object with any date, how can I check that the object fits the interval? 还有另一个带有任何日期的DateTime对象,如何检查该对象是否适合间隔? So if the interval is 30 min, then dates such as 2016-10-04 12:00:00 and 2016-10-04 14:30:00 would be valid, while 2016-10-04 12:05:10 is not valid anymore? 因此,如果间隔为30分钟,则日期(例如2016-10-04 12:00:002016-10-04 14:30:00将是有效的,而2016-10-04 12:05:10则无效有效了吗?

I could not find many resources for this, except some solutions for MySQL and/or converting dates to unix timestamps, doing division in seconds and checking remainder. 除了一些针对MySQL的解决方案和/或将日期转换为unix时间戳,以秒为单位进行除法并检查余数之外,我找不到很多资源。 Was wondering if there is a nicer and better solution for this? 想知道是否有更好,更好的解决方案吗?

Thanks! 谢谢!

Try: 尝试:

 $minutes = date('i',strtotime($inputDate);
 $seconds = date('s',strtotime($inputDate);
 if((($minutes == '00') || ($minutes == '30')) && ($seconds == '00')){
     // do stuff
 }

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

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