0

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

Amirreza Moradi
  • 221
  • 1
  • 4
  • 19
  • Working with dates and date formats it's not that black and white. You can convert them to timestamps and then compare them. They are a lot of ways to do it. – CritingZ May 17 '19 at 11:20
  • i just dont know howcan i find the two dates that user send between my two dates, thats it!! – Amirreza Moradi May 17 '19 at 11:26
  • Possible duplicate of [How to calculate the difference between two dates using PHP?](https://stackoverflow.com/questions/676824/how-to-calculate-the-difference-between-two-dates-using-php) – Dave May 17 '19 at 11:41
  • i just update my question again :) – Amirreza Moradi May 17 '19 at 11:47

1 Answers1

0

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 ) ) {

}
Sjors
  • 1,205
  • 1
  • 8
  • 24