doutun9179 2017-03-29 08:59
浏览 73
已采纳

计算不起作用的日期之间的差异并给出巨大的错误

Hi I'm making an online coupon system for mobile devices. The coupons are for a limited time active and I have to do a check on the coupons. I use date(d/m/Y H:i:s) for just showing the date and time, and I also have a expire date which is a just a string that I later convert into a date.

This is how I do the check if the coupon is expired:

if ($date1B > $date2B) {
   echo "<script>alert('Expired coupon!!');</script>";
}

Now what I want is to calculate the days when the coupon will be expired.

This is what I found on W3Schools, but the example below uses date_create(), so you make an custom date and time. I already have 2 dates and times.

$date1 = date("d-m-Y  H:i:s");
$date2 = date_format($date2A, 'd-m-Y H:i:s');
$diff = date_diff($date1,$date2);
echo $diff->format("%a days %h Hours %i Minute %s Seconds ");

When I replace variables with the existing variables I get these errors:

date_diff() expects parameter 1 to be DateTimeInterface, string given in

and

Call to a member function format() on boolean in

The full .PHP page:

<?php
   date_default_timezone_set("America/Curacao");

   $date1A = date("d/m/Y  H:i:s");
   $date1B = date("dmYHis");
   $date2B = "27032017042100";
   $date2A = date_create_from_format('dmYHis', $date2B);

   echo "Datum 1: " . $date1A . "<br>" ;
   echo "Datum 1: " . $date1B . "<br><br>";
   echo "Datum 2: " . date_format($date2A, 'd/m/Y H:i:s') . "<br>";
   echo "Datum 2: " . $date2B . "<br>";

   if ($date1B > $date2B) {
      echo "<script>alert('Klaar!!');</script>";
   }

   $date1 = date("d/m/Y  H:i:s");
   $date2 = date_format($date2A, 'd/m/Y H:i:s');
   $diff = date_diff($date1,$date2);
   echo $diff->format("%a days %h Hours %i Minute %s Seconds ");
?>
  • 写回答

3条回答 默认 最新

  • dtio35880438 2017-03-29 09:59
    关注

    Updated .PHP file

    I fixed the errors.. This is the updated .PHP page:

    <?php
    
        date_default_timezone_set("America/Curacao");
        $date1A = date("d-m-Y H:i:s");
        $date1 = date_create($date1A);
        echo date_format($date1,"d-m-Y H:i:s");
    
        echo "<br>";
    
        $date2B = "31032017042100";
        $date2A = date_create_from_format('dmYHis', $date2B);
        $final = date_format($date2A, 'd-m-Y H:i:s');
        $date2 = date_create($final);
        echo date_format($date2,"d-m-Y H:i:s");
    
        echo "<br>";
    
        $diff = date_diff($date1, $date2);
        echo $diff->format("%R %a days %h Hours %i Minute %s Seconds");
    
        if ($date1 > $date2) {
           echo "<script>alert('Coupon Expired!!');</script>";
        }
    
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作