1

I need help to get month count between 2 dates:

$date1 = "2013-01-15";
$date2 = "2013-04-15";

I need to get the result of: 4, which are Jan, Feb, Mar, and Apr from the above variables.

Is there a simple way to get the result instead by exploding those dates?

Thank you.

Marvo
  • 17,845
  • 8
  • 50
  • 74
EquinoZ
  • 21
  • 1
  • 3
  • possible duplicate of [Elegant way to get the count of months between two dates?](http://stackoverflow.com/questions/4233605/elegant-way-to-get-the-count-of-months-between-two-dates) – mindas Mar 31 '13 at 23:16

3 Answers3

2

if running php >= 5.3 try this:

$date1 = new DateTime("2013-01-15");
$date2 = new DateTime("2013-04-15");
var_dump($date1->diff($date2)->m + 1); // int(4)
ElefantPhace
  • 3,806
  • 3
  • 20
  • 36
1
$datetime1 = new DateTime('2013-01-15');
$datetime2 = new DateTime('2013-04-15');
$interval = $datetime1->diff($datetime2);
echo $interval->format('%M%');
zloctb
  • 10,592
  • 8
  • 70
  • 89
1
<?php

 $d1 = new DateTime('2013-01-15');
 $d2 = new DateTime('2013-04-15');

$interval = $d2->diff($d1);

$interval = ($interval->format('%m months')) +1;

?>

http://www.php.net/manual/en/class.dateinterval.php

alwaysLearn
  • 6,882
  • 7
  • 39
  • 67