I have a PHP function that returns bool based on whether the time is currently in any number of pre-defined “hotzones.” Timezone is America/Chicago (UTC – 0600). The following works:
$d = 60*60; /* duration of hotzone */
$o = -(3*24+18)*3600; /* offset to bring UNIX epoch to 12a Sun local*/
$curTime = (time()-$o)%604800; /* time since 12a Sun */
/* Hotzones */
$hotTime = array();
$hotTime[0 ] = (0*24+11)*3600; /* 11a Sun */
$hotTime[1 ] = (0*24+18)*3600; /* 6p Sun */
$hotTime[2 ] = (2*24+19)*3600; /* 7p Tue */
$hotTime[3 ] = (3*24+ 6)*3600; /* 6a Wed */
$hotTime[4 ] = (3*24+11)*3600; /* 11a Wed */
$hotTimes = count($hotTime);
for ($i = $hotTimes-1; $i>=0; $i--) {
if (($curTime > $hotTime[$i])&&($curTime < $hotTime[$i]+$d)) {
return true;
}
}
return false;
However, I have to manually update for Daylight Savings twice per year, and I have to think there’s a more natural, elegant way to do this than the hackish “offset” I calculated. Has anyone come across a nicer way to do this, that takes into account DST?
You can use the
DateTimeclass for this:You should not use UNIX timestamps for such things. Using DateTime is the preferred way for that. Also read the comment of Sven. (thanks)