将日期从数据库转换为不同的时区 [英] Converting date from database to a different timezone

查看:119
本文介绍了将日期从数据库转换为不同的时区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个网站,允许人们张贴的东西。当它们发布时,它被插入到数据库中,并且数据库添加一个时间戳。

I have a website that allows people to post things. When they post, it gets inserted into the database and the database adds a timestamp.

当我在本地机器上托管数据库时,时间戳记在我的时区。我的新托管网站已设置为GMT时间。

When I hosted the database on my local machine, the timestamps were in my time zone. My new hosting website has it set to GMT time.

如何让我的时区显示时间?我已尝试添加

How can I make it display the time in my time zone? I've tried adding

date_default_timezone_set('America/New_York');

到我的视图,只是在标签下,但是没有帮助。任何想法?

to my views just under the tag, but that doesn't help. Any ideas?

推荐答案

// Set the time zone
$dateTimeZone = new DateTimeZone('America/New_York');

// Create the datetime and set the timestamp
$dateTime = new DateTime();
$dateTime->setTimestamp($timestamp);

// Convert it
$dateTime->setTimeZone($dateTimeZone);

echo $dateTime->format('m/d/Y H:i:s');

应该工作

这篇关于将日期从数据库转换为不同的时区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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