doucan8521 2017-08-06 04:41
浏览 78
已采纳

如何使用php计算图书馆罚款截止日期前的日期

This is my code of library fines calculation

$currentdate= date('Y/m/d');

$start = new DateTime($returndate=$row['due_date']); <-- from database
$end = new DateTime($currentdate);
$days= $start->diff($end, true)->days;
$fines = $days > 0 ? intval(floor($days)) * ₱10 : 0;

I want to calculate borrowing books every day after due date cost ₱10 but it just counted the day and display like this sorry I'm beginner in php coding thank you

  • 写回答

1条回答 默认 最新

  • dpaal28266 2017-08-06 04:55
    关注

    $end is currentday and because of that it will calculate a fine even if currentdate is less than duedate.
    What you need is a if currentday is more than duedate, then calculate the fine.

    $currentdate = date('Y/m/d');
    $start = new DateTime($returndate=$row['due_date']); <-- from database
    $end = new DateTime($currentdate);
    
    if(strtotime($currentdate) > strtotime($returndate)){
        $days= $start->diff($end, true)->days;
        $fines = $days > 0 ? intval(floor($days)) * 10 : 0;
    }
    

    strtotime() will convert a date in string format to UNIX time (integer).

    You can test the code here: https://3v4l.org/fl3IM

    Edit:

    Method 1.

    $currentdate = date('Y/m/d');
    $start = new DateTime($returndate=$row['due_date']); <-- from database
    $end = new DateTime($currentdate);
    
    $fines = 0;
    if(strtotime($currentdate) > strtotime($returndate)){
        $days= $start->diff($end, true)->days;
        $fines = $days > 0 ? intval(floor($days)) * 10 : 0;
    }
    

    Now it will show a fine of 0 until someone has passed due date. (may look cluttered in the page)


    Method 2.

    $currentdate = date('Y/m/d');
    $start = new DateTime($returndate=$row['due_date']); <-- from database
    $end = new DateTime($currentdate);
    
    if(strtotime($currentdate) > strtotime($returndate)){
        $days= $start->diff($end, true)->days;
        $fines = $days > 0 ? intval(floor($days)) * 10 : 0;
    }
    
    //some other code I guess...  
    
    if(isset($fines)) echo $fines;
    

    Here $fines is not set and will not be echoed unless someone is passed due date. This method probably will look the best on the page.
    But I don't know exactly how your page is set up.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 51单片机中C语言怎么做到下面类似的功能的函数(相关搜索:c语言)
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起