3

I wanted to calculate the difference between two dates in month with PHP but it seems like there is a bug somewhere.

$datetime1 = new DateTime(date('Y-m-d')); $datetime2 = new DateTime(MyObject->getDate()); echo($datetime1->format('d/m/Y')); echo($datetime2->format('d/m/Y)); 

Result:

29/01/2016 27/01/2015 $dateInterval = $datetime1->diff($datetime2); echo($dateInterval->format(%m months); 

Result:

0 months 

Why is that? What am i doing wrong ?

4
  • 1
    In the case you describe, there is no difference in months (they're both January), you'll find that the date interval shows the year property contains 1 as there is one year difference between the dates. Commented Jan 29, 2016 at 11:44
  • ok.. I thought it was the difference between 2 dates in months Commented Jan 29, 2016 at 11:45
  • print_r($dateInterval) can help you understand how the class DateInterval works. Commented Jan 29, 2016 at 11:48
  • Possible duplicate of PHP Difference in months between two dates? Commented Jan 29, 2016 at 11:56

5 Answers 5

6
$currentDateTime = new DateTime; $dateTimeInTheFuture = new DateTime(MyObject->getDate()); $dateInterval = $dateTimeInTheFuture->diff($currentDateTime); $totalMonths = 12 * $dateInterval->y + $dateInterval->m; echo $totalMonths; 
Sign up to request clarification or add additional context in comments.

Comments

2

Calculate months between two dates:

For PHP >=5.3 you can use DateTime diff that returns a DateInterval object as below.

$d1 = new DateTime("2013-12-09"); $d2 = new DateTime("2014-03-17"); var_dump($d1->diff($d2)->m); var_dump($d1->diff($d2)->m + ($d1->diff($d2)->y*12)); 

If you don’t have PHP 5.3 or higher, you can use strtotime() function to get timestamps, the number of seconds between any date and January 1 1970 00:00:00.

$d1 = "2013-12-09"; $d2 = "2014-03-17"; echo (int)abs((strtotime($d1) - strtotime($d2))/(60*60*24*30)); 

http://www.tricksofit.com/2013/12/calculate-the-difference-between-two-dates-in-php

Comments

0
// @link http://www.php.net/manual/en/class.datetime.php $d1 = new DateTime(date('Y-m-d')); $d2 = new DateTime(MyObject->getDate()); // @link http://www.php.net/manual/en/class.dateinterval.php $interval = $d2->diff($d1); $interval->format('%m months'); 

Comments

0

DateTime::diff returns relative values, with exception of days. So, to calculate the absolute difference in months, you have to use:

$datetime1->diff($datetime2)->format('%y')*12+$datetime1->diff($datetime2)->format('%m'); 

Comments

0

You are just missed Single quote termination,

$datetime1 = new DateTime(date('Y-m-d')); $datetime2 = new DateTime(MyObject->getDate()); echo($datetime1->format('d/m/Y')); echo($datetime2->format('d/m/Y'));//You are missing single quote here 

I am also try with this code,

<?php $datetime1 = date_create('2009-10-11'); $datetime2 = date_create('2009-12-13'); echo($datetime1->format('d/m/Y')); echo "<br/>"; echo($datetime2->format('d/m/Y')); $dateInterval = $datetime1->diff($datetime2); //print_r(arrayColumn($dateInterval,'m')); echo "<br>Month are :".$dateInterval->format('%m'); exit; ?> 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.