如何使用DateTime类在PHP中转换时区?

时间:2022-10-28 20:55:13

I am trying to convert time between current time to UTC and UTC to current time zone.

我试图将当前时间与UTC和UTC之间的时间转换为当前时区。

Here is what I have done:

这是我做的:

$schedule_date = new DateTime($triggerOn, new DateTimeZone('UTC') );
$triggerOn =  $schedule_date->format('Y-m-d H:i:s');

echo $triggerOn;

The output value does not change the only thing that changes in format.

输出值不会更改格式中唯一更改的内容。

the string $triggerOn was generated based on America/Los_Angeles timezone

字符串$ triggerOn是根据America / Los_Angeles时区生成的

This is how my string looks like before and after:

这是我的字符串之前和之后的样子:

BEFORE    04/01/2013 03:08 PM
AFTER     2013-04-01 15:08:00

So the issue here is that DateTime does not convert to UTC.

所以这里的问题是DateTime不会转换为UTC。

3 个解决方案

#1


81  

What you're looking for is this:

您正在寻找的是:

$triggerOn = '04/01/2013 03:08 PM';
$user_tz = 'America/Los_Angeles';

echo $triggerOn; // echoes 04/01/2013 03:08 PM

$schedule_date = new DateTime($triggerOn, new DateTimeZone($user_tz) );
$schedule_date->setTimeZone(new DateTimeZone('UTC'));
$triggerOn =  $schedule_date->format('Y-m-d H:i:s');

echo $triggerOn; // echoes 2013-04-01 22:08:00

#2


13  

You are consuming the date/time and setting the time zone correctly, however before formatting the datetime, you are not setting the desired output timezone. Here is an example which accepts a UTC time zone, and converts the date/time to the America/Los_Angeles time zone:

您正在使用日期/时间并正确设置时区,但在格式化日期时间之前,您没有设置所需的输出时区。以下是接受UTC时区的示例,并将日期/时间转换为America / Los_Angeles时区:

<?php
$original_datetime = '04/01/2013 03:08 PM';
$original_timezone = new DateTimeZone('UTC');

// Instantiate the DateTime object, setting it's date, time and time zone.
$datetime = new DateTime($original_datetime, $original_timezone);

// Set the DateTime object's time zone to convert the time appropriately.
$target_timezone = new DateTimeZone('America/Los_Angeles');
$datetime->setTimeZone($target_timezone);

// Outputs a date/time string based on the time zone you've set on the object.
$triggerOn = $datetime->format('Y-m-d H:i:s');

// Print the date/time string.
print $triggerOn; // 2013-04-01 08:08:00

#3


2  

Create the date using the local timezone, then call DateTime::setTimeZone() to change it.

使用本地时区创建日期,然后调用DateTime :: setTimeZone()来更改它。

#1


81  

What you're looking for is this:

您正在寻找的是:

$triggerOn = '04/01/2013 03:08 PM';
$user_tz = 'America/Los_Angeles';

echo $triggerOn; // echoes 04/01/2013 03:08 PM

$schedule_date = new DateTime($triggerOn, new DateTimeZone($user_tz) );
$schedule_date->setTimeZone(new DateTimeZone('UTC'));
$triggerOn =  $schedule_date->format('Y-m-d H:i:s');

echo $triggerOn; // echoes 2013-04-01 22:08:00

#2


13  

You are consuming the date/time and setting the time zone correctly, however before formatting the datetime, you are not setting the desired output timezone. Here is an example which accepts a UTC time zone, and converts the date/time to the America/Los_Angeles time zone:

您正在使用日期/时间并正确设置时区,但在格式化日期时间之前,您没有设置所需的输出时区。以下是接受UTC时区的示例,并将日期/时间转换为America / Los_Angeles时区:

<?php
$original_datetime = '04/01/2013 03:08 PM';
$original_timezone = new DateTimeZone('UTC');

// Instantiate the DateTime object, setting it's date, time and time zone.
$datetime = new DateTime($original_datetime, $original_timezone);

// Set the DateTime object's time zone to convert the time appropriately.
$target_timezone = new DateTimeZone('America/Los_Angeles');
$datetime->setTimeZone($target_timezone);

// Outputs a date/time string based on the time zone you've set on the object.
$triggerOn = $datetime->format('Y-m-d H:i:s');

// Print the date/time string.
print $triggerOn; // 2013-04-01 08:08:00

#3


2  

Create the date using the local timezone, then call DateTime::setTimeZone() to change it.

使用本地时区创建日期,然后调用DateTime :: setTimeZone()来更改它。