0

In php how to check if one date (given as string) differs to another date at least three month (unable to find any examples):

$date1 = "2013-11-05";
$date2 = "2013-11-19";
//both dates is in form yyyy.mm.dd
differsThreeMonths($date1,$date2) { ???? return $differs; }
differsThreeMonths("2013-11-05","2014-05-02");//true 
differsThreeMonths("2014-01-01","2014-04-01");//true 
differsThreeMonths("2014-01-01","2014-03-31");//false
differsThreeMonths("2013-12-01","2014-01-15");//false
etc

Thank you

Daniel Li
  • 14,976
  • 6
  • 43
  • 60
user2301515
  • 4,903
  • 6
  • 30
  • 46
  • 2
    This is a start: http://stackoverflow.com/questions/676824/how-to-calculate-the-difference-between-two-dates-using-php – karthikr Nov 11 '13 at 15:20
  • simplest way is to convert both dates into unix timestamps by using `strtotime` then calculate the number of seconds contained in 3 months then substract the dates and compare the difference against the number of seconds from those 3 months. – Twisted1919 Nov 11 '13 at 15:22

3 Answers3

4
$datetime1 = new DateTime('2013-07-01 12:00:00');
$datetime2 = new DateTime('2013-11-11 12:00:00');
$interval = $datetime1->diff($datetime2);
if(($interval->m>=3) || ($interval->y > 0))
echo "at least 3 months";
else
echo "Not greater than 3 months";
Alix Axel
  • 151,645
  • 95
  • 393
  • 500
  • Change `$interval->m > 3` to `$interval->m >= 3`, since exactly 3 month also count. – Peon Nov 11 '13 at 15:28
2
$datetime1 = new DateTime('2009-10-11');
$datetime2 = new DateTime('2009-10-13');
$interval = $datetime1->diff($datetime2);
var_dump($interaval);

http://www.php.net/manual/en/datetime.diff.php

cetver
  • 11,279
  • 5
  • 36
  • 56
1

Try:

function differsThreeMonths($date_str1, $date_str2) {
    if (strlen($date_str1) > 0 && strlen($date_str2) > 0) {
        $date1 = new DateTime($date_str1);
        $date2 = new DateTime($date_str2);
        $since_start = $date1->diff($date2);
        return (($since_start->y > 0) || ($since_start->m >= 3));
    } else {
        return -1; // ERROR CODE HERE
    }
}
Alix Axel
  • 151,645
  • 95
  • 393
  • 500
Daniel Li
  • 14,976
  • 6
  • 43
  • 60
  • @DanielLi: It's meant for both actually. One uses variables, the other uses constants but the faulty logic it's exactly the same. – Alix Axel Nov 11 '13 at 15:40
  • @DanielLi: Yes they do, yet the output is "Not greater then 3 month". This is because you are only comparing months and not also years. – Alix Axel Nov 11 '13 at 15:43
  • @DanielLi: As for your last edit, it's also not accurate. It's seconds and not milliseconds and 3 months is more often than not not 7776000 seconds - even a day is not always 86400 seconds. – Alix Axel Nov 11 '13 at 15:49
  • I would use your prior logic and just add another condition, like this: `return (($diff->y > 0) || ($diff->m >= 3));`. – Alix Axel Nov 11 '13 at 15:50
  • @DanielLi: No it isn't: http://ideone.com/yE6G9Q. Also, 1 hour = 3600 seconds, 1 day = 86400. My prior comment about a day not always being 86400 seconds was due to occasional DST changes and UTC corrections. – Alix Axel Nov 11 '13 at 15:55