简体   繁体   English

PHP strtotime EST来回转换

[英]PHP strtotime EST back and forth conversion

For EST time, I've set: 对于EST时间,我设置:

date_default_timezone_set("America/New_York");

As the page loads, I'm getting EST time via: 当页面加载时,我通过以下方式获得EST时间:

$time = time();

The problem is when I convert strings back and forth between timestamps and datetime format: 问题是当我在时间戳和日期时间格式之间来回转换字符串时:

10/31/2012 7:30pm 1351729800 | 10/31/2012, 8:30 pm
11/2/2012 7:30pm 1351902600 | 11/02/2012, 8:30 pm
11/3/2012 8:00pm 1351990800 | 11/03/2012, 9:00 pm
11/7/2012 8:00pm 1352336400 | 11/07/2012, 8:00 pm
11/9/2012 8:00pm 1352509200 | 11/09/2012, 8:00 pm
11/10/2012 8:00pm 1352595600 | 11/10/2012, 8:00 pm

I'm expecting these date & times to be the same. 我期待这些日期和时间是一样的。

The first section (before the "|") is simply strings, such as "10/31/2012 7:30pm" and the strtotime("10/31/2012 7:30pm EST"). 第一部分(在“|”之前)只是字符串,例如“10/31/2012 7:30 pm”和strtotime(“10/31/2012 7:30 pm EST”)。

The section (after the "|") is date() of the previous strtotime() value. 部分(在“|”之后)是前一个strtotime()值的date()。

What can I do to convert from string to time (strtotime) and double check that the date format returned is the same as the string input? 我该怎么做才能从字符串转换为时间(strtotime)并仔细检查返回的日期格式是否与字符串输入相同?

Thanks 谢谢

I'd say that this is because currently New York is on daylight savings time - EDT rather than EST. 我要说这是因为目前纽约正处于夏令时 - EDT而不是EST。 This affects things like so: 这会影响到这样的事情:

date_default_timezone_set("America/New_York");
strtotime("10/31/2012 7:30pm"); // translates to "Wed, 31 Oct 2012 19:30:00 -0400"
strtotime("10/31/2012 7:30pm EDT"); // translates to "Wed, 31 Oct 2012 19:30:00 -0400"
strtotime("10/31/2012 7:30pm EST"); // translates to "Wed, 31 Oct 2012 20:30:00 -0400"

The quick fix is probably to not add the timezone to the string, strtotime() will use the correct default timezone you set. 快速修复可能是不将时区添加到字符串中, strtotime()将使用您设置的正确默认时区。

You can be a bit more exact about how your date is being parsed by using the DateTime createFromFormat function: 通过使用DateTime createFromFormat函数,您可以更准确地了解如何解析DateTime

$date = DateTime::createFromFormat('m/d/Y g:ia', "10/31/2012 7:30pm");
echo $date->format('U');

Alternatively if you wait until November the problem will resolve itself :-) 或者,如果你等到11月,问题将自行解决:-)

Some times you need to set and forget a time in a PHP script and moving to a new server could change your hard work. 有时您需要在PHP脚本中设置并忘记时间,并且转移到新服务器可能会改变您的辛勤工作。 if you are doing date with string to time for example add your time zone code to your string like New Yourk wiht daylight savings time: 'EDT' 如果您正在使用字符串来表示日期,例如将您的时区代码添加到您的字符串中,例如New Yourk夏令时:'EDT'

echo date('g:i a',strtotime('10:59 EDT'));// 10:59 am

or if you want to extract the amount of seconds an specific time has, lets say 10:59pm, use something like: 或者如果你想提取一个特定时间的秒数,让我们说晚上10:59,使用类似的东西:

echo gmdate('g:i a',strtotime('January 1 1970 10:59pm GMT'));//82740 seconds //converted to 10:59pm

the latter will give you (12+10 hours(*60) and 59 minutes)*60 in seconds, and the gmdate function will echo the GMT time from what ever seconds you pass to the function.. if you try date instead you will, most likely, not get 10:59 unless you live in the GMT time zone (Greenwich Mean Time). 后者将给你(12 + 10小时(* 60)和59分钟)* 60秒,并且gmdate函数将从你传递给函数的几秒钟回显GMT时间..如果你尝试日期而不是你将最有可能的是,除非你住在格林威治标准时间(格林威治标准时间),否则不会得到10:59。

This time zone stuff can get very confusing, so be very careful and mess around with it when having a very specific plan..like converting someones time to yours, etc. 这个时区的东西可能会变得非常混乱,所以当有一个非常具体的计划时要非常小心并搞乱它。就像将某些人的时间转换为你的等等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM