Giedrius
Giedrius

Reputation: 1390

PHP - Check if given Date fits a given interval

If I have a DateTime interval, such as:

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

And another DateTime object with any date, how can I check that the object fits the interval? 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?

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. Was wondering if there is a nicer and better solution for this?

Thanks!

Upvotes: 0

Views: 220

Answers (1)

Austin
Austin

Reputation: 1627

Try:

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

Upvotes: 1

Related Questions