Search code examples
datelaravel-5php-7date-comparison

php compare start and end dates between two dates


Hi everyone:) i develop the rent module that rent the flats to users, i just want to check if the flat rent in two request dates, for examples , the flat rent from 2019-05-07 to 2019-08-07, when this flat rents again i want to check the start and end date not in the range of two dates i said above

i just write a condition to check the request dates and rent dates,

if($rent->endContract >= $request->startContract ||
  $rent->startContract <= $request->endContract) {
}

but this condition not work, for example when i want to rent similar flat to user from 2019-08-08 to 2019-11-08 in condition not work even though the dates are correct, please help me to find the best solution thanks alot

Update:

i just want to know the ranges of two dates that user sent to server is not in the range that store in database


Solution

  • PHP has a method called strtotime (https://php.net/strtotime). With this you can convert a date to a timestamp, which is an integer. You can compare an integer with another integer. Try this:

    if (strtotime( $rent->endContract ) >= strtotime( $request->startContract ) ||
        strtotime( $rent->startContract ) <= strtotime( $request->endContract ) ) {
    
    }