Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP: Changing UTC time to Pacific (PST/PDT) [duplicate]

Tags:

php

I have a Datetime object and get the time with the following:

$today = date('Y-m-d H:i:s', $temp['date']->getTimeStamp());

I would like to be able to convert this into California's current time, but I need to account for PST and PDT for the time offset. It could be -7 or -8 hours depending on the time of the year. How can I dynamically obtain $today for the correct Pacific time?

like image 345
musicliftsme Avatar asked Dec 29 '25 14:12

musicliftsme


1 Answers

You can simply use strtotime to change the timezone, like this:

date_default_timezone_set('Etc/GMT'); //Make sure the time is GMT
$today = date('Y-m-d H:i:s');

echo $today."\n";

$today_utc = date('Y-m-d H:i:s', strtotime( $today." GMT+3"));

echo $today_utc."\n";

$today_9 = date('Y-m-d H:i:s', strtotime( $today." GMT-9"));

echo $today_9;

demo: http://sandbox.onlinephpfunctions.com/code/f08c0c69b415464bbee68fd1cdaaf1e7f4f1872b

So in your case your code could be:

$today = date('Y-m-d H:i:s', $temp['date']->getTimeStamp());
$today = date('Y-m-d H:i:s', strtotime( $today." GMT-9"));
like image 138
Green Black Avatar answered Dec 31 '25 04:12

Green Black



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!