PHP/MySQL时间戳和时区 [英] PHP/MySQL timestamp and timezones

查看:90
本文介绍了PHP/MySQL时间戳和时区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我使用MySQL中的NOW()函数获取当前时间戳,是否可以通过php捕获该字段并在不同时区提供该时间?基本上将当前时区中的当前时间转换为另一个时区?

If I grab the current timestamp using the NOW() function in MySQL can I grab that field via php and give that time in different time zones? Basically converting current time in the current timezone to another timezone?

推荐答案

您可以使用

You can use the DateTimeZone class:

$gmt = new DateTimeZone("GMT");
$datetimeInGMT = new DateTime($now, $gmt);

它也采用continent/city形式的位置,例如Europe/London.

It also takes locations in the form continent/city, e.g. Europe/London.

如果您的日期时间不是UTC,则可以使用setTimezone:

If your datetime is non-UTC, you can use setTimezone:

$datetimeInGMT = new DateTime($now, new DateTimeZone("America/New_York"));
$datetimeInGMT->setTimezone(new DateTimeZone("GMT"));

这篇关于PHP/MySQL时间戳和时区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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