Daylight Saving Time
I have always thought there would have to be a programatic way of determining when daylight saving started and finished. I know that it’s an arbitrary date and it’s subject to change, but I knew there would be a way to somehow look it up.
I’ve been looking for this before and never really came across anything and even when looking at the date/time functions in
php, and even though gettimezone retrieves the current timezone (including whether or not DST is currently in force or not), it didn’t do much to solve the “when does it start next” problem.
Then one day I was playing in the backend of a website and I noticed there was two lines of text under the timezone information that was intriguing:
I opened up the trust text editor and went fishing inside the server.
Now, I’m pretty sure I’ve seen timezone_transitions_get before, but for whatever reason, something never clicked. It’s EXACTLY what I was looking for!
DateTimeClass there are a myriad of functions to do with what the offset from UTC is, what effect DST has on the timezone, is it in effect now, amongst others; including get_transitions. This returns all the DST transitions for the time zone, past and future.
The only caveat here is your version of php would have to be reasonably up-to-date in the event that a local government changes when DST will start and finish. and although that probably wouldn’t be too often, it’s something that still must be considered.
This snippet I’ve created here isn’t perfect, it’s just bits and pieces cobbled together and it does need a good cleaning out. However, I’ve included it here and it’s available to you if you’d like to use it under the MIT license.
You may notice several things:
- the screenshot above is taken from the backend of WordPress
- that information in the backend of WordPress is actually incorrect (DST doesn’t begin at 1am for that timezone)
- the code itself creates a shortcode for displaying the data
- the code is the actual code used by this site
- I have attempted to correct the the DST conversion that WordPress has in it, but I haven’t got it quite figured out (just yet)
Anyway, it’s a work in progress.