PHP 时间戳转换为 DateTime
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12038558/
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 Timestamp into DateTime
提问by JREAM
Do you know how I can convert this to a strtotime, or a similar type of value to pass into the DateTimeobject?
您知道如何将其转换为 strtotime 或类似类型的值以传递给DateTime对象吗?
The date I have:
我的日期:
Mon, 12 Dec 2011 21:17:52 +0000
What I've tried:
我试过的:
$time = substr($item->pubDate, -14);
$date = substr($item->pubDate, 0, strlen($time));
$dtm = new DateTime(strtotime($time));
$dtm->setTimezone(new DateTimeZone(ADMIN_TIMEZONE));
$date = $dtm->format('D, M dS');
$time = $dtm->format('g:i a');
The above is not correct. If I loop through a lot of different dates its all the same date.
以上是不正确的。如果我遍历很多不同的日期,它的日期都是相同的。
回答by FtDRbwLXw6
You don't need to turn the string into a timestamp in order to create the DateTimeobject (in fact, its constructor doesn't even allow you to do this, as you can tell). You can simply feed your date string into the DateTimeconstructor as-is:
您不需要将字符串转换为时间戳来创建DateTime对象(事实上,正如您所知,它的构造函数甚至不允许您这样做)。您可以简单地将日期字符串按DateTime原样输入构造函数:
// Assuming $item->pubDate is "Mon, 12 Dec 2011 21:17:52 +0000"
$dt = new DateTime($item->pubDate);
That being said, if you do have a timestamp that you wish to use instead of a string, you can do so using DateTime::setTimestamp():
话虽如此,如果您确实希望使用时间戳而不是字符串,则可以使用DateTime::setTimestamp():
$timestamp = strtotime('Mon, 12 Dec 2011 21:17:52 +0000');
$dt = new DateTime();
$dt->setTimestamp($timestamp);
Edit (2014-05-07):
编辑(2014-05-07):
I actually wasn't aware of this at the time, but the DateTimeconstructor doessupport creating instances directly from timestamps. According to this documentation, all you need to do is prepend the timestamp with an @character:
我当时实际上并不知道这一点,但DateTime构造函数确实支持直接从时间戳创建实例。根据此文档,您需要做的就是在时间戳前加上一个@字符:
$timestamp = strtotime('Mon, 12 Dec 2011 21:17:52 +0000');
$dt = new DateTime('@' . $timestamp);
回答by John Slegers
While @drrcknlsnis correct to assert there are multiple ways to convert a time string to a datatime, it's important to realize that these different ways don't deal with timezones in the same way.
虽然@drrcknlsn断言有多种方法可以将时间字符串转换为数据时间是正确的,但重要的是要意识到这些不同的方法不会以相同的方式处理时区。
Option 1 : DateTime('@' . $timestamp)
选项1 : DateTime('@' . $timestamp)
Consider the following code :
考虑以下代码:
date_format(date_create('@'. strtotime('Mon, 12 Dec 2011 21:17:52 +0800')), 'c');
The strtotimebit eliminates the time zone information, and the date_createfunction assumes GMT (Europe/Brussels).
该strtotime位消除了时区信息,date_create函数假定 GMT ( Europe/Brussels)。
As such, the output will be the following, no matter which server I run it on :
因此,无论我在哪个服务器上运行它,输出都将如下所示:
2011-12-12T13:17:52+00:00
Option 2 : date_create()->setTimestamp($timestamp)
选项2: date_create()->setTimestamp($timestamp)
Consider the following code :
考虑以下代码:
date_format(date_create()->setTimestamp(strtotime('Mon, 12 Dec 2011 21:17:52 +0800')), 'c');
You might expect this to produce the same output. However, if I execute this code from a Belgian server, I get the following output :
您可能期望这会产生相同的输出。但是,如果我从比利时服务器执行此代码,则会得到以下输出:
2011-12-12T14:17:52+01:00
Unlike the date_createfunction, the setTimestampmethod assumes the time zone of the server ('Europe/Brussels'in my case) rather than GMT.
与date_create函数不同的是,该setTimestamp方法假定服务器的时区('Europe/Brussels'在我的例子中)而不是 GMT。
Explicitly setting your time zone
显式设置您的时区
If you want to make sure your output matches the time zone of your input, it's best to set it explicitly.
如果您想确保您的输出与您输入的时区相匹配,最好明确设置它。
Consider the following code :
考虑以下代码:
date_format(date_create('@'. strtotime('Mon, 12 Dec 2011 21:17:52 +0800'))->setTimezone(new DateTimeZone('Asia/Hong_Kong')), 'c')
Now, also consider the following code :
现在,还要考虑以下代码:
date_format(date_create()->setTimestamp(strtotime('Mon, 12 Dec 2011 21:17:52 +0800'))->setTimezone(new DateTimeZone('Asia/Hong_Kong')), 'c')
Because we explicitly set the time zone of the output to match that of the input, both will create the same (correct) output :
因为我们明确设置输出的时区以匹配输入的时区,所以两者都会创建相同(正确)的输出:
2011-12-12T21:17:52+08:00
回答by forsberg
Probably the simplest solution is just:
可能最简单的解决方案就是:
DateTime::createFromFormat('U', $timeStamp);
Where 'U' means Unix epoch. See docs: http://php.net/manual/en/datetime.createfromformat.php
其中“U”表示 Unix 时代。请参阅文档:http: //php.net/manual/en/datetime.createfromformat.php
回答by ingenious
it is my solution:
这是我的解决方案:
function changeDateTimezone($date, $from='UTC', $to='Asia/Tehran', $targetFormat="Y-m-d H:i:s")
{
$date = new DateTime($date, new DateTimeZone($from));
$date->setTimeZone(new DateTimeZone($to));
return $date->format($targetFormat);
}

