简体   繁体   English

Cakephp 3 - 将时间从本地时区转换为 UTC

[英]Cakephp 3 - convert time from local timezone to UTC

i have a problem about the correct timezone when patching an entity.修补实体时,我对正确的时区有疑问。 My post request looks like this:我的帖子请求如下所示:

   [
    'event_date' => '11.07.2019',
    'start_time' => '15:00',
    'end_time' => '16:00'
   ]

In my form I have a calendar and two fields where the user can choose the start and end time.在我的表单中,我有一个日历和两个字段,用户可以在其中选择开始和结束时间。 Now these values are the timezone of the user.现在这些值是用户的时区。 What I want is to convert those values to UTC and save them with my entity which has two DATETIME and one DATE fields.我想要的是将这些值转换为 UTC 并将它们与我的实体一起保存,该实体具有两个DATETIME和一个DATE字段。

To save it correctly without any validation error I have the following code for modifying the data before patching:为了在没有任何验证错误的情况下正确保存它,我有以下代码用于在修补前修改数据:

$event_date = new \DateTime($data['event_date']);

$start_time = new \DateTime($data['start_time']);
$end_time = new \DateTime($data['end_time']);

$start_time->setDate($event_date->format('Y'), $event_date->format('m'), $event_date->format('d'));
$end_time->setDate($event_date->format('Y'), $event_date->format('m'), $event_date->format('d'));

$data['start_time'] = $start_time;
$data['end_time'] = $end_time;

$data['event_date'] = $event_date;

But as you can see from the debug it converts it wrong.但是正如您从调试中看到的那样,它转换错了。 Cause I'm in the europe timezone it should have 2 hours less.因为我在欧洲时区,所以应该少2 小时

[
    'event_date' => object(DateTime) {
        date => '2019-07-11 00:00:00.000000'
        timezone_type => (int) 3
        timezone => 'UTC'
    },
    'start_time' => object(DateTime) {
        date => '2019-07-11 15:00:00.000000'
        timezone_type => (int) 3
        timezone => 'UTC'
    },
    'end_time' => object(DateTime) {
        date => '2019-07-11 16:00:00.000000'
        timezone_type => (int) 3
        timezone => 'UTC'
    }
]

I configured my bootstrap like this:我这样配置我的引导程序:

Time::setToStringFormat('d.m.Y H:i');
Time::setJsonEncodeFormat('d.m.Y H:i');

FrozenTime::setToStringFormat('d.m.Y H:i');
FrozenTime::setJsonEncodeFormat('d.m.Y H:i');

Date::setToStringFormat('d.m.Y');
Date::setJsonEncodeFormat('d.m.Y');

FrozenDate::setToStringFormat('d.m.Y');
FrozenDate::setJsonEncodeFormat('d.m.Y');


Type::build('time')
    ->useImmutable();
Type::build('date')
    ->useImmutable();

Type::build('datetime')
    // ->useImmutable()
    ->useLocaleParser()
    ->setLocaleFormat('d.m.Y H:i:ss');

Can someone help me here?有人可以在这里帮助我吗? I'm a bit confused...我有点困惑...

Well, you're not using the Date and DateTime objects from Cake at all: \\DateTime this mean it is the plain php DateTime object.好吧,您根本没有使用 Cake 中的 Date 和 DateTime 对象: \\DateTime这意味着它是普通的 php DateTime对象。

Fix that and use the correct objects and see what happens then.修复它并使用正确的对象,然后看看会发生什么。 Also make sure you configured your default time zone correctly.还要确保您正确配置了默认时区。 Have a read: https://book.cakephp.org/3.0/en/core-libraries/time.html读一读: https : //book.cakephp.org/3.0/en/core-libraries/time.html

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

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