Nate
Nate

Reputation: 28384

How to check if a time offset is in daylight savings 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?

Upvotes: 5

Views: 6464

Answers (3)

João Gonçalves
João Gonçalves

Reputation: 4002

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?

Upvotes: 8

Gordon
Gordon

Reputation: 316989

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?

Upvotes: 6

Celada
Celada

Reputation: 22261

If all you have is "-5", then you don't have enough information to make that determination. It would be preferable if you could arrange to have access to the name of the time zone the user is located in (such as "America/New_York").

Upvotes: 2

Related Questions