php无法分析时间字符串
我想得到db中的日期和当前日期之间的差异 这是我的代码:php无法分析时间字符串,php,Php,我想得到db中的日期和当前日期之间的差异 这是我的代码: $current_date = new \DateTime(date('Y-m-d H:i:s')); foreach ($notifs as $notif) { $created_notif = new \DateTime($notif->created); $diff = date_diff($current_date, $created_notif);
$current_date = new \DateTime(date('Y-m-d H:i:s'));
foreach ($notifs as $notif) {
$created_notif = new \DateTime($notif->created);
$diff = date_diff($current_date, $created_notif);
if ($diff->y > 0) {
$notif->time = $diff->y . " year";
}
//..
}
这就是我得到的错误:
DateTime::_构造():无法分析时间字符串(2016年5月17日)
08:54)在位置0(1)处:
为了不说我没有搜索过,我厌倦了DateTime::createFromFormat
像这样的事情:
$format = 'Y-m-d H:i:s';
$current_date = new \DateTime(date($format));
foreach ($notifs as $notif) {
$created_notif =\DateTime::createFromFormat($format,$notif->created);
$diff = date_diff($current_date, $created_notif);//line 32
debug($created_notif);//line 33
die();
这就是我得到的:
警告(2):date_diff()要求参数2为DateTimeInterface,
给定布尔值[APP/Controller\AdminController.php,第32行]
\src\Controller\AdminController.php(第33行)
假的
我需要你的帮助我想,
$notif->创建的
是17/05/2016 08:54
,所以此时你应该使用
$created_notif = \DateTime::createFromFormat('d/m/Y H:i', $notif->created);
这就是为什么你没有得到一个
DateTime
对象,这就是为什么date\u diff
不起作用的原因。我假设,$notif->created
是17/05/2016 08:54
,所以此时你应该使用
$created_notif = \DateTime::createFromFormat('d/m/Y H:i', $notif->created);
这就是为什么你没有得到一个
DateTime
对象,这就是为什么date\u diff
不起作用的原因。why*\*作为DateTime
的前缀?@FrayneKonok-namespacking如果我不使用它,他不知道它的格式是什么$notif->created
?看起来像是dd/mm/yyyyy
谢谢@MarkBaker.why*\*作为DateTime
??@FrayneKonok-namespacking如果我不使用它,他不知道它创建的$notif->的格式是什么?看起来像是dd/mm/yyyyy
谢谢@MarkBaker。var\u dump($created\u notif)
应该是false
。我更改了格式,就像你说的d/m/Y H:I和我得到了相同的错误,你从var\u dump($notif->created)
?@MateiMihai'17/05/2016 08:54'问题来自`$format='d/m/Y H:I'$当前\日期=新\日期时间($格式)`@MateiMihai感谢您修复了拼写错误。太遗憾了:)var_dump($created_notif)
应该是false
。我更改了格式,就像你说的d/m/Y H:I和我得到了与你从var_dump($notif->created)
?@MateiMihai'17/05/2016 08:54'中得到的相同的错误,问题来自“$format='d/m/Y H:I'$当前\日期=新\日期时间($格式)`@MateiMihai感谢您修复了拼写错误。真可惜:)