0

i have date

$d1='2014-02-01';
$d2='2013-11-01';

i want if i minus $d2 - $d1 = i get difference of month = -3 and if $d1 - $d2 = 3 ,

Any one can help, sorry for my bad english

John Conde
  • 217,595
  • 99
  • 455
  • 496
Mamen
  • 1,322
  • 5
  • 21
  • 44

1 Answers1

2
$d1= new DateTime('2014-02-01');
$d2= new DateTime('2013-11-01');
$diff = $d1->diff($d2);
echo $diff->format("%r%m");

Demo

John Conde
  • 217,595
  • 99
  • 455
  • 496
  • 1
    I went with lowercase r as I don't think they want thew `+` sign when the diff is positive – John Conde Apr 01 '14 at 12:58
  • but if $diff=$d2->diff($d1); the result isn't -3 – Mamen Apr 01 '14 at 13:00
  • The date math may work backwards of how you are expecting. But that is easily worked around by switching the operands: `$diff=$d1->diff($d2);` The results will be consistent. – John Conde Apr 01 '14 at 13:02