从 PHP 中删除一个小时 (FROM_UNIXTIME)
请您建议如何从 unixtime 中删除一个小时。
我有一个unixtime,我需要在转换为正常时间之前删除一个额外的小时。您能建议如何做到这一点吗?
另外,Unixtime 是否受到 GMT 时间变化的影响?
Could you please advice how to remove an hour from unixtime.
I have a unixtime and i need to remove an extra hour before converting to normal time. Could you please advice how to do this?
Also, is Unixtime affected by the GMT time changes?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Unix时间戳以秒为单位,一小时有3600秒(60分钟,每60秒=60*60=3600),所以只需减去:
您也可以使用
strtotime
来完成相同的任务:或者如果
$timestamp
只是“现在”,你可以调用strtotime
没有第二个参数:所以你似乎在寻找 GMT 时间,问题是 GMT 可以包括夏令时 (DST),而 PHP 中的“GMT”日期函数实际上返回 UTC 时间,没有 DST 的概念。幸运的是,您可以使用 PHP 检测是否是夏令时,并基本上进行适当调整。
使用
gmdate
获取 UTC 时间:使用
I
:gmdate('U')
在 DST 期间会落后 GMT 时间一个小时,因此您需要给它+3600
秒,或者在 DST 时为 1 小时,所以把这个 一起:Unix timestamps are measured in seconds, there are 3600 seconds in an hour (60 minutes, each with 60 seconds = 60*60 = 3600), so just subtract:
You can also use
strtotime
to accomplish the same:Or if
$timestamp
is simply "now" you can callstrtotime
without the second parameter:So you seem to be looking for GMT time instead, the trouble is GMT can include Daylight Savings Time (DST), and the "GMT" date functions in PHP actually return UTC time, which has no concept of DST. Luckily you can detect if it's DST using PHP, and basically adjust appropriately.
Get UTC time using
gmdate
:Detect if it's DST using
I
:gmdate('U')
will trail GMT time during DST by an hour, thus you need to give it+3600
seconds, or 1 hour when it's DST, so putting this together: