如何检查时间偏移是否在夏令时? [英] How to check if a time offset is in daylight savings time?

查看:64
本文介绍了如何检查时间偏移是否在夏令时?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的 CMS 允许我访问用户相对于 UTC/GMT 时间的时间偏移量.因此,如果用户已将他们的时区设置为 EST,则此偏移量将为-5".

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.

所以,我的问题是,如何确定给定的时间偏移(如-5")是否需要添加一个小时?

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

推荐答案

您不能仅从偏移量中得出这一点,因为偏移量适用于整个纬度,并且没有统一 DST 生效的国家/地区.例如在澳大利亚,夏令时在昆士兰、北领地或西澳大利亚,但在其他任何地方.您必须定位您的用户,然后使用PHP 的 DateTimeZone API 那个国家是否有夏令时.

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.

另请参阅如何自动检测用户的时区?

这篇关于如何检查时间偏移是否在夏令时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆