Chonchol Mahmud
Chonchol Mahmud

Reputation: 2735

How should I write this specific condition in php?

The Challenge is:

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 do I write for this condition?

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

Upvotes: 1

Views: 117

Answers (5)

csanjose
csanjose

Reputation: 164

<?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";
?>

Upvotes: 0

Charlie
Charlie

Reputation: 23778

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

}

Upvotes: 1

Nisha
Nisha

Reputation: 685

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.

Upvotes: 1

deceze
deceze

Reputation: 522016

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';

Upvotes: 3

abhinsit
abhinsit

Reputation: 3272

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

Upvotes: 0

Related Questions