在symfony 4中将字符串转换为时间戳
我在控制器中有一个变量recover from ajax和recover在symfony 4中将字符串转换为时间戳,symfony,symfony4,Symfony,Symfony4,我在控制器中有一个变量recover from ajax和recover dd($gethour); "Sat Oct 26 2019 00:00:04 GMT+0200 (heure d’été d’Europe centrale)" $from = \DateTime::createFromFormat('m-d-y H:i:s', $gethour); dd($form); 结果:错误 我的问题是如何将字符串转换为基本小时中显示的时间戳Pass是一种类型的时间戳 date("m-d-y
dd($gethour);
"Sat Oct 26 2019 00:00:04 GMT+0200 (heure d’été d’Europe centrale)"
$from = \DateTime::createFromFormat('m-d-y H:i:s', $gethour);
dd($form);
结果:错误
我的问题是如何将字符串转换为基本小时中显示的时间戳Pass是一种类型的时间戳
date("m-d-y H:i:s", strtotime($gethour));
这种方法对我来说是正确的。如果你能从一个真正的时间戳开始,它是一个很长的数字,那就更好了。但除此之外,你可以这样做:
$gethour = "Sat Oct 26 2019 00:00:04 GMT+0200 (heure d’été d’Europe centrale)";
$chunks = explode(' ', $gethour);
$from = new \DateTime($chunks[1] . ' ' . $chunks[2] . ' ' . $chunks[3] . ' ' . $chunks[4]);
$from->setTimezone(new DateTimeZone($chunks[5]));
echo $from->format('d-m-Y H:i:s P'); // 26-10-2019 09:00:04 +02:00
谢谢你,这很有效,但只是为了恢复,在树枝后如何恢复只是几个小时和分钟不知道你的问题。但是在twig中,可以使用日期过滤器从DateTime对象中获取字符串:{{post.published_at | date(“H:i”)}