How to check if a time offset is in daylight savin

2019-02-23 05:43发布

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?

3条回答
爱情/是我丢掉的垃圾
2楼-- · 2019-02-23 06:08

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?

查看更多
别忘想泡老子
3楼-- · 2019-02-23 06:18

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").

查看更多
手持菜刀,她持情操
4楼-- · 2019-02-23 06:19

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?

查看更多
登录 后发表回答