Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if a time offset is in daylight savings time?

Tags:

timezone

php

time

The CMS I'm using allows me to access the user's time offset from UTC/GMT time. So, if the user has set their timezone to EST, then this offset will be "-5."

What I'm doing right now when I need to display the current time is something like:

date('M j Y h:i A', time() + $offset*3600)

This works, except when daylight savings time is in affect for the user, then it is an hour behind.

So, my question is, how I can determine whether a given time offset (like "-5") needs to have an hour added to it?

like image 935
Nate Avatar asked Jun 24 '12 22:06

Nate


People also ask

Does offset change with daylight savings?

An offset is the number of hours or minutes a certain time zone is ahead of or behind GMT**. A time zone's offset can change throughout the year because of Daylight Saving Time.

How do you know if it's daylight savings time?

Daylight Saving Time - When do we change our clocks? Most of the United States begins Daylight Saving Time at 2:00 a.m. on the second Sunday in March and reverts to standard time on the first Sunday in November.

Does UTC offset change with daylight savings?

Daylight Saving Time has never been used here. Clocks do not change in UTC, Time Zone. There is no previous DST change in Time Zone.

How do I get a time zone offset?

Definition and Usage. getTimezoneOffset() returns the difference between UTC time and local time. getTimezoneOffset() returns the difference in minutes. For example, if your time zone is GMT+2, -120 will be returned.


2 Answers

http://php.net/manual/en/function.date.php

I (capital i) | Whether or not the date is in daylight saving time | 1 if Daylight Saving Time, 0 otherwise.

Does this help?

like image 184
João Gonçalves Avatar answered Nov 15 '22 01:11

João Gonçalves


You cant derive this from the offset alone because the offset applies to the whole latitude and countries on that do not have uniform DST in effect. For instance in Australia, Daylight Saving Time is not observed in Queensland, the Northern Territory or Western Australia but everywhere else. You'd have to geolocate your users and then check with PHP's DateTimeZone API whether there is DST in that country.

Also see How to automatically detect user's timezone?

like image 37
Gordon Avatar answered Nov 14 '22 23:11

Gordon