PHP timestamp in DateTime

Do you know how I can convert this to strtotime or a similar value type to a DateTime object?

Date I have:

Mon, 12 Dec 2011 21:17:52 +0000 

What I 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 incorrect. If I go through many different dates, it will all be on the same date.

+46
php datetime
Aug 20 '12 at 13:25
source share
3 answers

You do not need to turn the string into a timestamp to create a DateTime object (in fact, its constructor does not even allow you to do this, as you can tell). You can simply pass the date string to the DateTime as-is constructor:

 // Assuming $item->pubDate is "Mon, 12 Dec 2011 21:17:52 +0000" $dt = new DateTime($item->pubDate); 

If you have a timestamp that you want to use instead of a string, you can do this using DateTime::setTimestamp()

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

Edit (2014-05-07):

I did not know about it at that time, but the DateTime constructor supports instantiating directly from timestamps. According to this documentation , all you have to do is add a timestamp with the @ symbol:

 $timestamp = strtotime('Mon, 12 Dec 2011 21:17:52 +0000'); $dt = new DateTime('@' . $timestamp); 
+88
Aug 20 2018-12-12T00:
source share

While @drrcknlsn is correct to say that there are several ways to convert a time string to a date, it is important to understand that these different methods are not related to the same as time.




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 excludes time zone information, and the date_create function assumes GMT ( Europe/Brussels ).

Thus, the output will be as follows: 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 can expect this to produce the same result. 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 server’s time zone ( 'Europe/Brussels' in my case), not GMT.




Explicit time zone setting

If you want to make sure that your result matches the time zone of your input, it is 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 consider also the following code:

 date_format(date_create()->setTimestamp(strtotime('Mon, 12 Dec 2011 21:17:52 +0800'))->setTimezone(new DateTimeZone('Asia/Hong_Kong')), 'c') 

Since we explicitly set the time zone of the output according to what is on the input, both will produce the same (correct) output:

 2011-12-12T21:17:52+08:00 
+12
Feb 20 '16 at 16:28
source share

Perhaps the simplest solution:

 DateTime::createFromFormat('U', $timeStamp); 

Where "U" stands for Unix era. See Docs: http://php.net/manual/en/datetime.createfromformat.php

+3
Sep 10 '17 at 7:05
source share



All Articles