尽管时间戳正确,但 PHP 日期显示错误的时间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29526129/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP date showing wrong time despite the timestamp being correct
提问by user3754277
I'm having a problem with the PHP date function which I've never had a problem with before.
我在使用 PHP 日期函数时遇到了问题,我以前从未遇到过这个问题。
The timestamp is entirely correct, however for some bizarre reason date() is outputting a time which does not correspond.
时间戳是完全正确的,但是由于一些奇怪的原因 date() 输出的时间不对应。
I have the following timestamp (and this is definitely the correct one - when I echo it out onto the page, as well as in the database, it shows as being correct):
我有以下时间戳(这绝对是正确的 - 当我将其回显到页面以及数据库中时,它显示为正确的):
464400
Yet when I use the following line of code:
然而,当我使用以下代码行时:
<?php echo date("H:i",$timestamp); ?>
I'm getting a time of 4 am? If I paste the timestamp into a timestamp converter website, then it shows the time should in fact be 9am.
我的时间是凌晨 4 点?如果我将时间戳粘贴到时间戳转换器网站,那么它显示的时间实际上应该是上午 9 点。
I'm completely stuck, this has never happened to me before & this problem has only just come up recently - the code hasn't been changed and everything seemed to be working correctly before.
我完全被困住了,这以前从未发生在我身上,这个问题最近才出现 - 代码没有改变,之前一切似乎都正常工作。
Does anyone have any experience with this issue? Any help would be appreciated.
有没有人有这个问题的经验?任何帮助,将不胜感激。
回答by exussum
That time stamp is is 9am GMT timezone, if you are in another timezone then you will need to adjust it accordingly.
该时间戳是格林威治标准时间上午 9 点,如果您在另一个时区,则需要相应地调整它。
http://php.net/manual/en/function.date-default-timezone-set.php
http://php.net/manual/en/function.date-default-timezone-set.php
date_default_timezone_set('Europe/London');
or even better in your php.ini
甚至在你的 php.ini 中更好
http://php.net/manual/en/datetime.configuration.php
http://php.net/manual/en/datetime.configuration.php
date.timezone="Europe/London"
Or you can use more specifically GMT instead of Europe/London (which has DST)
或者您可以更具体地使用格林威治标准时间而不是欧洲/伦敦(有夏令时)
回答by Vijay Balan
try this method will work
试试这个方法会奏效
for time zone http://php.net/manual/en/timezones.php
对于时区 http://php.net/manual/en/timezones.php
code
代码
<?php
date_default_timezone_set('Asia/Kolkata');
$dt2=date("Y-m-d H:i:s");
echo $dt2;
?>
回答by user789456
try this
尝试这个
// set default timezone
date_default_timezone_set('UTC');
//define unix timestamp
$timestamp = 1456778973;
// output
echo date('d M Y H:i:s',$timestamp);
Try this converter too http://freeonlinetools24.com/
也试试这个转换器http://freeonlinetools24.com/