dtftao7249656 2016-05-11 06:13
浏览 7
已采纳

我该如何在php中编写这个特定条件?

The Challenge is:

  • If the book is returned on or before the expected return date, no fine will be charged (i.e.: fine=0).
  • If the book is returned after the expected return day but still within the same calendar month and year as the expected return date, fine=15 Hackos × (the number of days late).
  • If the book is returned after the expected return month but still within the same calendar year as the expected return date, the fine=500 Hackos × (the number of months late).
  • If the book is returned after the calendar year in which it was expected, there is a fixed fine of 10000 Hackos.

And My code is:

<?php
$expectedDay   = "6";
$expectedMonth = "6";
$expectedYear  = "2015";

$returnDay     = "9";
$returnMonth   = "6";
$returnYear    = "2015";
if ($expectedDay >= $returnDay && $expectedMonth >= $returnMonth && $expectedYear >= $returnYear) {
    echo "Fine = 0";
}elseif ($expectedDay < $returnDay && $expectedMonth == $returnMonth && $expectedYear == $returnYear) {
    $fine = 15 * ($returnDay-$expectedDay);
    echo "Fine = ".$fine;
}elseif (($expectedDay <= $returnDay || $expectedDay >= $returnDay) && $expectedMonth < $returnMonth && $expectedYear == $returnYear) {
    $fine = 500 * ($returnMonth-$expectedMonth);
    echo "Fine = ".$fine;
}else{
    echo "Fine = 1000";
}

?> 

Its running well.But failed when the input is:

$expectedDay   = "28";
$expectedMonth = "2";
$expectedYear  = "2015";

$returnDay     = "15";
$returnMonth   = "4";
$returnYear    = "2015";

How i write for this condition? Thanks in advanced.

Note: This is not a business logic.It is just Practice purposes.I am beginner in PHP.

  • 写回答

5条回答 默认 最新

  • duanboshe0001 2016-05-11 06:22
    关注

    You do not have to compare dates when the month condition is checked.

    <?php
    $expectedDay   = "6";
    $expectedMonth = "6";
    $expectedYear  = "2015";
    
    $returnDay     = "9";
    $returnMonth   = "6";
    $returnYear    = "2015";
    $returnDate = new DateTime($returnDay.'-'.$returnMonth.'-'.$returnYear);
    $expectedDate = new DateTime($expectedDay.'-'.$expectedMonth.'-'.$expectedYear);
    
    if ($returnDate <= $expectedDate) {
        echo "Fine = 0";
    }elseif ($expectedDay < $returnDay && $expectedMonth == $returnMonth && $expectedYear == $returnYear) {
        $fine = 15 * ($returnDay-$expectedDay);
        echo "Fine = ".$fine;
    }elseif ($expectedMonth < $returnMonth && $expectedYear == $returnYear) {
        $fine = 500 * ($returnMonth-$expectedMonth);
        echo "Fine = ".$fine;
    }else{
        echo "Fine = 1000";
    }
    
    ?> 
    

    Try that.

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

报告相同问题?

悬赏问题

  • ¥15 fluent的在模拟压强时使用希望得到一些建议
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 Web.config连不上数据库
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。
  • ¥15 怎么配置广告联盟瀑布流
  • ¥15 Rstudio 保存代码闪退