dongxiong5546 2017-09-19 06:20
浏览 96
已采纳

在布尔值上调用成员函数format()

I want to find the difference between two dates and I have used date_diff for the same. When format function is applied on date_diff object it returns an error.

Call to a member function format() on boolean

$field_value is fetched from the database and it's format is dd/mm/YYYY. When I hard-code the values for $field_value and $indexing_value the following code works.

Everything is running fine till line number 8. I have tried outputting the value of

$diff->format("%R%a")

and it is returning exact value but the code gives error near the if statement.

$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";
}

Please let me know what is wrong with the above code.

  • 写回答

2条回答 默认 最新

  • drgovyk64676 2017-09-19 06:24
    关注

    add condition to check whether you got the diff or not, as it returns false if there is error . Check manual for the same

    $diff = date_diff($indexing_value, $current_value);
    if ($diff) {
        if ($diff->format("%R%a") < 0) {
            echo "1";
        }else{
            echo "2";
        }   
    }
    

    You are getting error because for some values the diff is not calculated and have value False in $diff

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行