Ok, weird one.
Wrote a script to check XML feed if school is closed for my kids county. Each entry comes with an "effective date" node, which is the date the school closing announcement is effective.
What I was doing, was comparing if the effective date, is older than the current time, which means that after midnight, a closing on the current day would work. But what if they close school the evening/night beforehand?
I need to check if the effective date is greater than today, but starting at 4pm instead of midnight.
date_default_timezone_set('America/New_York');
$date_effective = $announcement->date_effective;
// format of xml feed datetime - 2018-02-02 00:00:00.000000
$date = new DateTime($date_effective);
$now = new DateTime();
// Here is where I need help, not sure how to word this
// if($date < $now || $date < $now - '8 hours') {
if($date < $now) {
// iterate through cases based on status
}
Use \DateTime::sub combined with a \DateInterval representing the time difference:
$now = new DateTime;
$then = (clone $now)->sub(DateInterval::createFromDateString('8 hours'));
if ($date < $now || $date < $then) {
// ...
}
(Note the use of clone
, because sub
modifies the object on which it's called.)
use strotime
instead of that
<?php
date_default_timezone_set('America/New_York');
$date_effective = $announcement->date_effective;
$date = strtotime($date_effective);
// since 1 hr is 3600 secs so multiply this by 8 which result to 28800
$now = strtotime('now') - 28800;
if($date < $now) {
// iterate through cases based on status
}