php

I was creating a program for a library and that is to have a system that will manage the borrowing of books. I was searching the net about comparing date in PHP. One good application for this PHP snippet about comparing a date is that when the date filled is an invalid date range(e.g. the date of borrow is greater than the date of return) or should I say wrong date input. When I searched the net the resources was so limited that some are even trash. So I decided to share the function I made and maybe this can help others or maybe there are any bugs somewhere on the code and have it corrected.

function dateComapare($dformat, $endDate, $beginDate) {
    $endDateNew = explode($dformat, $endDate);
    $endDateNewDay     =  $endDateNew[0];
    $endDateNewMonth   =  $endDateNew[1];
    $endDateNewYear    =  $endDateNew[2];
    $beginDateNew = explode($dformat, $beginDate);
    $beginDateNewDay     =  $beginDateNew[0];
    $beginDateNewMonth   =  $beginDateNew[1];
    $beginDateNewYear    =  $beginDateNew[2];
    $yrDiff = $beginDateNewYear - $endDateNewYear;
    if($yrDiff < 0 && $yrDiff != 0 ){
    return 'Year Exceeds!';
  }
  else{
    $mthDiff = $beginDateNewMonth - $endDateNewMonth;
    if($mthDiff < 0 && $mthDiff != 0){
      return 'Month Exceeds!';
    }
    else{
      $dayDiff = $beginDateNewDay - $endDateNewDay;
      if($dayDiff < 0 && $dayDiff != 0 ){
        return 'Date Exceeds!';
      }
      else{
        return 'Date OK';
      }
    }
  }
}



The code above will compare for the year first and then the month and then the date. If the given input is invalid then it prompts the error on which part of the borrow form was mistakenly filled up.

Did find the post very useful? Maybe you want to buy me a glass of beer!