Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert UNIX Timestamp to PST in PHP

Tags:

php

datetime

time

I am getting Date & Time in UNIX Timestamp. So wanted to convert it to "America/Los_Angeles" Timezone in dd-mm-yyyy HH:MM:SS format.

How to do this in PHP ?

e.g: Input: 1345014000 Output: 15-08-2012 00:00:00

like image 346
Shakul Saini Avatar asked Jan 29 '26 11:01

Shakul Saini


1 Answers

The date_default_timezone_set function can be used to set the current timezone within PHP. You need only call this once per file, as all calculations from then onwards will be done using that timezone. It accepts all valid timezone names, such as the one you mentioned.

The date function on the other hand, accepts a string explaining the format, and the timezone you wish to be converted. A full list of formatting options can be found at: http://php.net/manual/en/function.date.php

Usage in your situation would be as follows:

date_default_timezone_set("America/Los_Angeles");
$str=date('d-m-Y H:i:s',$timestamp); //Where $timestamp is the timestamp to be converted
like image 178
Death Avatar answered Jan 30 '26 23:01

Death



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!