【发布时间】:2017-09-19 14:56:54
【问题描述】:
我想找出两个日期之间的差异,我也使用了date_diff。在date_diff 对象上应用格式函数时,它会返回错误。
在布尔值上调用成员函数 format()
$field_value 从数据库中获取,格式为dd/mm/YYYY。当我对 $field_value 和 $indexing_value 的值进行硬编码时,以下代码有效。
在第 8 行之前一切都运行良好。我尝试输出
的值$diff->format("%R%a")
它返回的是准确的值,但是代码在 if 语句附近给出了错误。
$date = new DateTime();
$current_date = $date->format('d/m/Y');
$indexing_value = str_replace("/", "-", $field_value);
$current_value = str_replace("/", "-", $current_date);
$indexing_value = date_create($indexing_value);
$current_value = date_create($current_value);
$diff = date_diff($indexing_value, $current_value);
if ($diff->format("%R%a") < 0) {
echo "1";
} else {
echo "2";
}
请告诉我上面的代码有什么问题。
【问题讨论】: