PHP strtotime()函数错误1小时? [英] PHP strtotime() function wrong by 1 hour?

查看:349
本文介绍了PHP strtotime()函数错误1小时?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在将日期和时间插入MySQL数据库之前,我正在使用PHP将日期和时间转换为时间戳.

I am converting dates and times into timestamps using PHP before they are inserted into my MySQL database.

我的问题是,当我使用PHP的strtotime函数时,输出时间戳比实际时间晚-1小时.

My problem is that when i use PHP's strtotime function the output timestamp is -1 hour behind my actual time.

例如,考虑今天的日期:2010年7月21日.当我使用php代码时:

For example, considering todays date: 07/21/2010. When i use php code like:

<?php
$my_timestamp = strtotime("07/21/2010");
echo $my_timestamp;
?>

发回的时间戳为格林尼治标准时间MINUS 1小时.即我回来了:格林尼治标准时间2010年7月20日23:00:00而不是格林尼治标准时间2010年7月21日00:00:00.

The timestamp sent back is the GMT equivilent MINUS 1 hour. i.e. i get back: 20 Jul 2010 23:00:00 GMT instead of 21 Jul 2010 00:00:00 GMT.

我住在英国,所以我所在的时区是格林尼治标准时间(GMT).我已经使用date_default_timezone_set('Europe/London')在脚本中声明了我的时区,并且还确保将php.ini文件设置为'Europe/London'.

I live in the UK so my timezone is GMT. I have declared my timezone in the script using date_default_timezone_set('Europe/London') and i have also ensured that the php.ini file is set to 'Europe/London'.

这可能与夏时制有关吗?如何在不增加所有约会1小时的情况下解决问题?

Is this something to do with daylight savings time perhaps? How can i fix the problem without adding 1 hour to all my dates?

推荐答案

夏令时欧洲/伦敦时间不是格林尼治标准时间.您需要将其设置为UTC.

Europe/London time is not GMT time during Daylight Savings. You need to set it to UTC.

date_default_timezone_set('UTC');

date_default_timezone_set('GMT');可能有效,但正如肯尼斯(Kenneth)在下面的评论中指出的那样,它已被弃用.

date_default_timezone_set('GMT'); may work, but, as Kenneth notes in a comment below, it is deprecated.

这篇关于PHP strtotime()函数错误1小时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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