我想在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);
if ($diff->y > 0) {
$notif->time = $diff->y . " year";
}
//..
}
Run Code Online (Sandbox Code Playgroud)
这是我得到的错误:
DateTime :: __ construct():无法在位置0(1)处解析时间字符串(17/05/2016 08:54):
并且不要说我没有搜索过我 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();
Run Code Online (Sandbox Code Playgroud)
这就是我得到的:
警告(2):date_diff()期望参数2为DateTimeInterface,给定布尔值为[APP/Controller\AdminController.php,第32行]
\ src\Controller\AdminController.php(第33行)
假
拜托我需要你的帮忙
我认为,该$notif->created是17/05/2016 08:54如此,在这一点上,你应该使用
$created_notif = \DateTime::createFromFormat('d/m/Y H:i', $notif->created);
Run Code Online (Sandbox Code Playgroud)
这就是为什么你没有得到一个DateTime对象,这就是为什么date_diff不起作用.
| 归档时间: |
|
| 查看次数: |
694 次 |
| 最近记录: |