If I have the user's timezone (e.g: Australia/Sydney), how do I use PHP to find the date and time that daylight savings begins in their local time?
Asked
Active
Viewed 73 times
0
-
AFAIK PHP can tell you if daylight-saving applies on a certain date, but if you want to know when daylight-saving starts you'll need to use a service. If your requirement is limited you could find out and hard-code the dates. – Mar 03 '15 at 04:31
-
2I think this has been answered [here before](http://stackoverflow.com/questions/6155224/php-daylight-saving-time-detection) – phpPhil Mar 03 '15 at 04:31
-
@phpPhil No it didn't quite answer my question. That function simply tells you when daylight savings changes. It did however get an idea from it and will put it as an answer now. Thanks for the help! – Ben Sinclair Mar 03 '15 at 05:04
1 Answers
0
I ended up working it our based on some code from this ticket.
$tz = new DateTimeZone('Australia/Sydney');
$transitions = $tz->getTransitions();
if (is_array($transitions)) {
foreach ($transitions AS $k => $t){
// Look for current year
if (substr($t['time'],0,4) == date('Y')) {
// If isdst is set to 1, that means daylight savings starts on this date
if (!empty($t['isdst'])) {
$trans = $t;
// If isdst is empty, that means daylight savings has already started
// so I'll go back to the previous transition
} else {
$prev_k = $k - 1;
$trans = $transitions[$prev_k];
}
break;
}
}
}
echo '<pre>';
print_r($trans);
echo '</pre>';

Community
- 1
- 1

Ben Sinclair
- 3,896
- 7
- 54
- 94