2

I am implementing a real Estate application and I need to EXACTLY check the date difference between two dates. I will prefer for this a javascript jquery approach, but any other solution will be welcome.

My goal is to not allow users to enter dates where the difference is EXACTLY bigger than 2 (or 5) years.

I have tried, along with many others, this solution: How to get the months between 2 dates

Unfortunately, this is not what I really need.

When I do: The rent will start at 01-01-2017 And will end at 31-12-2018 I will get 24 months (exactly rented for 2 years), which is correct for my purpose.

But if I try: The rent will start at 01-01-2017 And will end at 01-01-2019 I will still get 24 months (but it is rented for 2 years and 1 day), which is not correct for my purpose.

Due to the law regulations in my country (Netherlands) there is for the contracts a big difference between the two above mentioned situations.

I will appreciate any help from your side.

Community
  • 1
  • 1
Franco
  • 2,309
  • 1
  • 11
  • 18

4 Answers4

1

As mentioned in the comment, add 2 years and subtract 1 day. Best to do it with DateTime class:

$dt = DateTime::createFromFormat('d-m-Y', '01-01-2017');

$dt->add(new DateInterval('P2Y'));
$dt->sub(new DateInterval('P1D'));

var_dump($dt->format('d-m-Y'));

This will produce the desired result:

string(10) "31-12-2018"
Kevin Kopf
  • 13,327
  • 14
  • 49
  • 66
  • Thanks for your reply. We don't know the end date the user will input. This can exceed the two years with every amount of days. – Franco Feb 03 '17 at 19:51
  • I gave your solution a try. If I use the start date as 01-01-2017 it will give me 31-12-2018 which is correct (2 years). But when I use a start date such as 15-01-2017 it will give me 14-12-2019 which are 3 years. Any idea? – Franco Feb 03 '17 at 20:21
  • My bad! It is of course logic that starting in the middle of the month will go to the next year. I will give this a try. Thanks for posting your answer – Franco Feb 03 '17 at 20:30
  • I'd say you have not thought through all the edge cases for this. For instance, I could not predict you allow users to input something like `15-01-2017`. Do you need to get the end of the next year no matter the starting date? Please specify specifically (pun-like tautology :))) – Kevin Kopf Feb 03 '17 at 21:08
  • No it is just fine. 15-01-2017 it will give me 14-12-2019 which are just the two rent years I have to check against. I have accepted your answer – Franco Feb 03 '17 at 22:38
0

This should be accurate and be aware of leap years etc...

<?php    
$date = date_create('02-01-2017');
date_add($date, date_interval_create_from_date_string('2 years'));
$twoyears = date_format($date, 'd-m-Y');
echo $twoyears; // 02-01-2019

$rentperiod = $twoyears->sub(DateInterval::createFromDateString('1 day'));
echo $rentperiod; //01-31-2019
Duane Lortie
  • 1,285
  • 1
  • 12
  • 16
  • object-oriented `DateTime` class is better, you at least do not have to keep leap years in mind – Kevin Kopf Feb 03 '17 at 18:58
  • Thanks for your reply. We don't know the end date the user will input. This can exceed the two years with every amount of days. In fact if in place of 01-01-2019 I do 02-01-2019 then the solution I am using gives me 25 months which is for my purpose correct. – Franco Feb 03 '17 at 19:54
  • Besides this will give me an error 'Calling sub on string'; – Franco Feb 03 '17 at 20:38
0

Here's an excellent function by SunilKmCharde, from the User Contributed Notes in the PHP manual:

<?php
  //////////////////////////////////////////////////////////////////////
  //PARA: Date Should In YYYY-MM-DD Format
  //RESULT FORMAT:
  // '%y Year %m Month %d Day %h Hours %i Minute %s Seconds'        =>  1 Year 3 Month 14 Day 11 Hours 49 Minute 36 Seconds
  // '%y Year %m Month %d Day'                                    =>  1 Year 3 Month 14 Days
  // '%m Month %d Day'                                            =>  3 Month 14 Day
  // '%d Day %h Hours'                                            =>  14 Day 11 Hours
  // '%d Day'                                                        =>  14 Days
  // '%h Hours %i Minute %s Seconds'                                =>  11 Hours 49 Minute 36 Seconds
  // '%i Minute %s Seconds'                                        =>  49 Minute 36 Seconds
  // '%h Hours                                                    =>  11 Hours
  // '%a Days                                                        =>  468 Days
  //////////////////////////////////////////////////////////////////////
  function dateDifference($date_1 , $date_2 , $differenceFormat = '%a' )
  {
      $datetime1 = date_create($date_1);
      $datetime2 = date_create($date_2);

      $interval = date_diff($datetime1, $datetime2);

      return $interval->format($differenceFormat);

  }
?>
alanlittle
  • 460
  • 2
  • 12
0

Javascript:


    function diff( d1, d2){
        x = new Date(d2-d1);
        return x.getUTCDate() + " " + x.getUTCMonth() + " " + (x.getUTCFullYear() - 1970);
    }

example:

a = new Date('01-01-2017');
b = new Date('01-01-2019');
diff(a,b);
"Days:1, Months: 0, Years: 2"
Julian
  • 111
  • 4