修补实体时,我对正确的时区存在疑问。我的帖子请求看起来像这样:
[
'event_date' => '11.07.2019',
'start_time' => '15:00',
'end_time' => '16:00'
]
在我的表单中,我有一个日历和两个字段,用户可以在其中选择开始时间和结束时间。现在,这些值就是用户的时区。我想要的是将这些值转换为UTC并将其保存到具有两个DATETIME
和一个DATE
字段的实体中。
要正确保存它而没有任何验证错误,我可以使用以下代码在修补之前修改数据:
$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;
但是从调试中可以看到,它将错误地转换。因为我在欧洲时区,所以应该少 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'
}
]
我这样配置了我的引导程序:
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');
有人可以在这里帮助我吗?我有点困惑...
答案 0 :(得分:1)
好吧,您根本没有使用Cake的Date和DateTime对象:const setStates = ({ state, setState }, newState) =>
setState(Object.assign({}, state, newState));
const [state, setState] = useState({
isFullscreen: false,
confirmDelete: false,
icon: props.icon,
iconUpload: null,
title: props.title,
description: props.description,
canShare: false,
canDefineHome: false,
canEditPortal: false,
canCopyPortal: false,
canViewAudit: false
});
useEffect(() => {
debugger;
// Enter here in the tests and assign
setStates(stateHooks, {
canShare: true,
canDefineHome: true,
canEditPortal: true,
canCopyPortal: true,
canViewAudit: true
});
}, []);
useEffect(() => {
// Here, always is false in test, setStates not working
console.log(stateHooks.state.canShare);
debugger;
}, [stateHooks.state.canShare]);
这意味着它是纯PHP article from Apple对象。
解决此问题并使用正确的对象,然后看看会发生什么。另外,请确保您正确配置了默认时区。阅读:DateTime