简体   繁体   English

将日期从本地时区转换为 UTC

[英]Converting a date from local timezone to UTC

I have a Laravel-based app that is used by people from various parts of the US.我有一个基于 Laravel 的应用程序,美国各地的人都在使用它。

I am capturing a timestamp in Javascript when the user takes a specific action, and then I am submitting that timestamp as form data, for the Laravel/PHP to process.当用户执行特定操作时,我在 Javascript 中捕获时间戳,然后我将该时间戳作为表单数据提交,供 Laravel/PHP 处理。

The timestamp that I am capture in Javascript is in typical "YYYY-MM-DD HH:MM:SS" format.我在 Javascript 中捕获的时间戳采用典型的“YYYY-MM-DD HH:MM:SS”格式。

I have the timezone the user is in stored in a database.我将用户所在的时区存储在数据库中。

I basically want to take that timestamp, and convert it to UTC time, so that all timestamps in the database are UTC.我基本上想获取该时间戳,并将其转换为 UTC 时间,以便数据库中的所有时间戳都是 UTC。

That is where I am struggling.那就是我挣扎的地方。

I have the following PHP code:我有以下 PHP 代码:

$defaultTime = request('submitted-time-stamp'); //In this case, we'll say 2022-12-21 12:01:01
$defaultTZ = $user->time_zone; //Translates to America/Denver

$utcTime = new DateTime($defaultTime);
$convertedTime = $utcTime1->setTimeZone(new DateTimeZone('UTC'));
$formattedTime = $convertedTime->format("Y-m-d H:i:s");

echo $formattedTime;

This code – it isn't producing any errors per sé... but it is showing the wrong time.这段代码——它没有产生任何错误……但它显示了错误的时间。 It's showing the time that it went in as, not the time converted to UTC.它显示的是它进入的时间,而不是转换为 UTC 的时间。

Basically, if I submit "2022-12-21 12:01:01" as the time, the converted time SHOULD be "2022-12-21 19:01:01", but it's still just echoing out "2022-12-21 12:01:01".基本上,如果我提交“2022-12-21 12:01:01”作为时间,转换后的时间应该是“2022-12-21 19:01:01”,但它仍然只是回应“2022-12- 21 12:01:01”。

What am I missing here?我在这里错过了什么?

setTimezone() changes the timezone of the object from whatever default it was created with. setTimezone()更改对象创建时使用的默认时区。 Ie, it means, "convert from the existing timezone to this new timezone."即,它的意思是“从现有时区转换到这个新时区”。 It does not mean, "interpret the time as if it were in this timezone."这并不意味着“将时间解释为就好像它在这个时区一样”。 If the original string didn't contain some sort of timezone identifier, then that default is whatever your PHP config says.如果原始字符串不包含某种时区标识符,那么该默认值就是您的 PHP 配置所说的任何内容。

$when = new DateTime('2022-12-21 12:01:01');
echo $when->getTimeZone()->getName();

This will be the same as:这将与以下内容相同:

echo date_default_timezone_get();

Which is probably not what you want unless all your users are in the same timezone as your server.这可能不是您想要的,除非您的所有用户都与您的服务器处于同一时区。

In order to create a DateTime object in a specific known timezone that is not the same as your server's default, you'll need one of two things -- either a timezone representation in the input string:为了在与服务器默认时区不同的特定已知时区中创建 DateTime 对象,您需要以下两件事之一——输入字符串中的时区表示:

$when = new DateTime('2022-12-21 12:01:01 America/New_York');

Or an explicit default timezone passed as a second parameter to the DateTime constructor:或者将显式默认时区作为第二个参数传递给 DateTime 构造函数:

$userDefaultTzStr = 'America/New_York'; // read this value from the database
$defaultTz = new DateTimeZone($userDefaultTzStr);
$when = new DateTime('2022-12-21 12:01:01', $defaultTz);

This latter method is (probably) preferred.后一种方法(可能)是首选。 If the input string contains any sort of timezone identifier, that will be used and the second parameter will be ignored.如果输入字符串包含任何类型的时区标识符,则将使用它并忽略第二个参数。 But if the input string does not contain any sort of timezone identifier, then the string will be interpreted as if it were in the indicated timezone.但是,如果输入字符串不包含任何类型的时区标识符,则该字符串将被解释为就好像它在指定的时区中一样。

Using Carbon it's very trivial.使用 Carbon 非常简单。

use Carbon\Carbon;

$date = Carbon::create(request('submitted-time-stamp'), $user->time_zone);
$date->tz('UTC');

echo $date->format('Y-m-d H:i:s');

It should be the same thing with Laravel's Date facade. Laravel 的Date facade 应该是一样的。

use Illuminate\Support\Facades\Date;

$date = Date::create(request('submitted-time-stamp'), $user->time_zone);
$date->tz('UTC');

echo $date->format('Y-m-d H:i:s');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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