我该如何在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.

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.

if($returnYear > $expectedYear){
    $fine = 1000;
}
else{
    if($returnMonth > $expectedMonth ){
         $fine = 500 * ($returnMonth-$expectedMonth);
    }
    else{
         if($returnDay>$expectedDay){
               $fine = $returnDay - $expectedDay;
          }
          else{
               $fine = 0;
          }

    }
}

echo $fine;

Above logic is built taken into consideration that months in number crossed will take complete month cost example :

if expected return date is 28/04/2016 and actual return date is 02/05/2016 then since month is changed the below algorithm takes fine @complete month.

If you want exact days difference to be considered for months/year calculation then we can write a different logic all together

Something along these lines:

function calculateLateFees(DateTime $deadline, DateTime $returned) {
    if ($returned <= $deadline) {
        return 0;
    }
    if ($returned->format('Y') > $deadline->format('Y')) {
        return 10000;
    }
    if ($returned->format('n') > $deadline->format('n')) {
        return ($returned->format('n') - $deadline->format('n')) * 500;
    }
    return $deadline->diff($returned)->days * 15;
}

$deadline = new DateTime('2015-02-28');
$returned = new DateTime('2015-04-15');

echo calculateLateFees($deadline, $returned), ' Hackos';

First, I must say that this is a very unfair calculation of fines. What if the expected day is 31.12.2016 and the book was returned 01.01.2017? According to your method the a fine of 10000 will be imposed for the lapse of one day just because it was across the margin of two separate years.

I would recommend you to calculate the fine according to the number of days late.

$lateDays = date_diff($expectedDate, $returnDate, false);

if ($lateDays > 0) {

   if ($lateDays < 30)
      $fine = 15 * $lateDays
   else
      if ($lateDays > 365)
         $fine = 10000
      else            
         $fine = 500 * $lateDays / 30

}

First php code, plz dont downvote a lot :)

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

$returnDay     = "9";
$returnMonth   = "6";
$returnYear    = "2015";

$expectedate=$expectedYear.'-'.$expectedMonth.'-'.$expectedDay;
$returndate=$returnYear.'-'.$returnMonth.'-'.$returnDay;

$expected=date_create($expectedate);
$return=date_create($returndate);

$interval=date_diff($expected, $return);
$valor=$interval->format('%R%a');

if ($valor>0) {

    if ($returnMonth==$expectedMonth && $returnYear==$expectedYear) echo "Fine=".(15*$valor);
    if ($returnMonth!=$expectedMonth && $returnYear==$expectedYear) echo "Fine=".(500*($returnMonth-$expectedMonth));
    if ($returnYear!=$expectedYear) echo "Fine=1000";

} else echo "Fine=0";
?>