简体   繁体   中英

How to change timezones and format time in PHP?

I have a variable like this,

$Timestamp = "Tue Mar 8 15:59:00 UTC-05:00 2016";

How do I change its format to YYYYMM-DD HH:MM AM/PM and change the timezone from UTC to Pacific Time using PHP?

PHP's DateTime object is pretty flexible.

$UTC = new DateTimeZone("UTC");
$TZ = new DateTimeZone("America/New_York");
$date = new DateTime( "2016-03-09 15:00:00", $UTC );
$date->setTimezone( $TZ );
echo $date->format('Y-m-d H:i:s');

Use php date:

date('Y/m/d H:i', $timestamp)
but your timestamp should be int:

 $timestamp = strtotime('22-09-2008');  

You can try following

$Timestamp = "Tue Mar 1 15:59:00 UTC-05:00 2016";

$datetime = new DateTime($Timestamp);
$datetime->format('Y-m-d H:i:s') . "\n";
$new_time = new DateTimeZone('Pacific/Apia');
$datetime->setTimezone($new_time);

//New formatted time    
echo $datetime->format('Y-m-d H:i:s');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM