PHP converts mktime result to UTC

I use this code to generate yesterday at PST (aka America / Los_Angeles) time. I cannot figure out how to convert the result to UTC.

date_default_timezone_set("America/Los_Angeles"); $time1 = date("Ymd H:i:s", mktime(0,0,0, date('n'), date('j')-1, date('Y'))); 

I tried this, but $ time1 is not datetime, this is a string. Therefore, the following will not work.

 $time1->setTimezone(new DateTimeZone("UTC")); 
+5
source share
1 answer

DateTime class can do anything for you

 $date = new DateTime(null, new DateTimeZone('America/Los_Angeles')); // will use now echo $date->format('d/m/YH:i:s'); //16/08/2016 16:13:29 $date->setTime(0,0,0); $date->modify('-1 day'); echo $date->format('d/m/YH:i:s'); // 15/08/2016 00:00:00 $date->setTimezone(new DateTimeZone('UTC')); echo $date->format('d/m/YH:i:s'); // 15/08/2016 07:00:00 
+5
source

All Articles