I want to convert the date I fetched from the database, which is generated via CURRENT_TIMESTAMP and stored in a timestamp column type, from GMT+8 to GMT+1.
$time = "2012-11-07 15:05:26"; // fetch from database
$date = new DateTime($time, new DateTimeZone('Europe/Berlin'));
echo $date->format('Y-m-d H:i:s');
However this will yield an output of “2012-11-07 15:05:26”, which I’m pretty sure is wrong.
What could be I’m missing in here?
First, you need to instantiate the datetime object with the original timezone. Then, after the datetime object is instantiated, adjust the timezone with
DateTime::setTimezone().See this code, where I’ve used
Asia/Hong_Kongas an example GMT+8 timezone:If all the original dates are always consistently meant as GMT+8, and your PHP application is set to use GMT+8 as well (set with
date_default_timezone_set(), for instance), there’s no need to pass the initialDateTimeZoneobject, as newly createdDateTimeobjects will automatically be created with that timezone.