日期不同功能在我的实时服务器中显示错误

时间:2017-05-04 07:36:42

标签: php codeigniter

这是我的块的代码,它给出了错误

$inti_date=strtotime($row->inti_date);
$inti_date=date('Y-m-d',$inti_date);
$diff=date_diff($today,$inti_date);
$temp = $diff->format("%a");

1 个答案:

答案 0 :(得分:0)

我假设您正在处理数据库中的日期,并希望从DateTime列中删除时间部分。

如果您注意到date_diff的原型

  

DateInterval date_diff(DateTimeInterface $ datetime1,DateTimeInterface $ datetime2 [,bool $ absolute = false])

date_diff要求日期为DateTimeInterface类型,因此需要使用DateTime类创建日期

// fake an object just for testing
$row = new stdClass();
$row->inti_date = '2017-05-01 10:10:10';

$inti_date = new DateTime($row->inti_date);
// I assume you were just after the data portion in both cases
// So set time to 0 o'clock
$inti_date->setTime(0,0,0);

$today = new DateTime('now');
// set time to 0 o'clock
$today->setTime(0,0,0);

$diff      = date_diff($today,$inti_date);
$temp      = $diff->format("%a");

echo $temp;

// or you could code the diff processing like this
$diff = $today->diff($inti_date);
echo $diff->format("%a");

2017年5月4日的结果

3