nick
nick

Reputation: 891

strtotime not putting the correct timezone from datetime obj

I have the following lines of code, I am converting an edt timezone to Asia/Shanghai (from php manual) and then formatting to display as a string. Now, if I do var_dump or getTimezone() on the datetime obj, I get something like (2), which is correct. But in the string, it still shows edt as the time zone. Not sure why it is doing so.. Anyone else experience this? Got any ideas? Thanks!

(1) $format = 'Y-m-d H:i:s';

$datetime = datetime::createfromformat($format,$appointment['appointment_datetime']);
///patient default shows time in China Standard 
$datetime = $datetime->setTimezone(new DateTimeZone('Asia/Shanghai'));

$display   =  date('l jS F Y \a\t\ g:ia T', strtotime($datetime->format($format)));

(2)object(DateTime)[8]

public 'date' => string '2016-08-02 05:00:00.000000' (length=26)
public 'timezone_type' => int 3
public 'timezone' => string 'Asia/Shanghai' (length=13)

Upvotes: 1

Views: 66

Answers (1)

rjdown
rjdown

Reputation: 9227

It's because you're using ->format to output the time as a string with no timezone, and then using date() to reformat it, which is undoing any timezone settings you tried to set.

You can just do $display = $datetime->format('l jS F Y \a\t\ g:ia T');

Upvotes: 2

Related Questions