PHP Timestamp into DateTime

…衆ロ難τιáo~ 提交于 2019-12-02 17:48:45

You don't need to turn the string into a timestamp in order to create the DateTime object (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 DateTime constructor as-is:

// 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():

$timestamp = strtotime('Mon, 12 Dec 2011 21:17:52 +0000');
$dt = new DateTime();
$dt->setTimestamp($timestamp);

Edit (2014-05-07):

I actually wasn't aware of this at the time, but the DateTime constructor does support creating instances directly from timestamps. According to this documentation, all you need to do is prepend the timestamp with an @ character:

$timestamp = strtotime('Mon, 12 Dec 2011 21:17:52 +0000');
$dt = new DateTime('@' . $timestamp);

While @drrcknlsn is 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.


Option 1 : DateTime('@' . $timestamp)

Consider the following code :

date_format(date_create('@'. strtotime('Mon, 12 Dec 2011 21:17:52 +0800')), 'c');

The strtotime bit eliminates the time zone information, and the date_create function assumes 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)

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_create function, the setTimestamp method assumes the time zone of the server ('Europe/Brussels' in my case) rather than 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

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

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);
    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!