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