Php convert date to timezone

I am converting this time and date:

Thu, 31 Mar 2011 02:05:59 GMT

To the following time and date format:

Monday March 28 2011 4:48:02 PM

I am using the following PHP code to accomplish this, but I want to convert all time zones to PST/PDT. I looked at the PHP manual and saw this date_default_timezone_set[] but I am not sure how to implement that into the code I have below.

$date = $messages[0]->CreationTime;
echo date['l F j Y g:i:s A I', strtotime[$date]]

asked Mar 31, 2011 at 16:27

0

I would not use date_default_timezone_set for general TZ conversions. [To clarify... if this is for display purposes, script wide, then using the default timezone is a reasonable thing to do.]

Instead, I would use something like:

$tz = new DateTimeZone['America/Los_Angeles'];

$date = new DateTime['Thu, 31 Mar 2011 02:05:59 GMT'];
$date->setTimezone[$tz];
echo $date->format['l F j Y g:i:s A I']."\n";

Kalzem

7,1566 gold badges53 silver badges77 bronze badges

answered Mar 31, 2011 at 16:35

MatthewMatthew

46.8k11 gold badges85 silver badges97 bronze badges

1

$date = $messages[0]->CreationTime;
date_default_timezone_set['America/Los_Angeles'];
echo date['l F j Y g:i:s A I', strtotime[$date]];

See this list for available timezones that get passed into the function

answered Mar 31, 2011 at 16:29

Mike BMike B

31.6k13 gold badges85 silver badges111 bronze badges

3

It’s really simple to convert a DateTime from one time zone to another in PHP. Just create a DateTime object using date & time to be converted as the first parameter and the original time zone as the second parameter. Then change the time zone to the desired one using the setTimezone method. That’s all!

$date = new DateTime['2018-04-11 12:00:00', new DateTimeZone['Europe/London']];
echo $date->format['Y-m-d H:i:sP'] . "\n";

$date->setTimezone[new DateTimeZone['Europe/Bucharest']];
echo $date->format['Y-m-d H:i:sP'] . "\n";

The above will output:

2018-04-11 12:00:00+01:00
2018-04-11 14:00:00+03:00

It provides code examples to convert the date and time to a different timezone in PHP.

This tutorial provides code examples to convert the date and time value from one timezone to another timezone using DateTime and DateTimeZone classes. We can always convert the date and time value from the active timezone to a different timezone.

The below-mentioned examples show the conversion of the given date and time from UTC timezone to the Los Angeles timezone. It further converts the date and time from the Los Angeles timezone to the London timezone.

// Get Timezone - UTC
$utcTimezone = new DateTimeZone[ 'UTC' ];

// Set time
$time = new DateTime[ '2020-06-03 10:45:15', $utcTimezone ];

echo $time->format[ 'Y-m-d H:i:s' ]; // 2020-06-03 10:45:15

// Get Timezone - Los Angeles
$laTimezone = new DateTimeZone[ 'America/Los_Angeles' ];

$time->setTimeZone[ $laTimezone ];

// Convert UTC to Los Angeles
echo $time->format[ 'Y-m-d H:i:s' ]; // 2020-06-03 03:45:15

// Get Timezone - London
$loTimezone = new DateTimeZone[ 'Europe/London' ];

$time->setTimeZone[ $loTimezone ];

// Convert Los Angeles to London
echo $time->format[ 'Y-m-d H:i:s' ]; // 2020-06-03 11:45:15

We can do multiple timezone conversions as shown above to show multiple clocks, keeping UTC as the standard timezone.

We can also use the function date_default_timezone_set to set the default timezone and use a different timezone to convert the date and time from the default timezone to the given timezone as shown below.

Chủ Đề