PHP DateTime::modify adding and subtracting months

dougmountain

New Member
I've been working a lot with the \[code\]DateTime class\[/code\] and recently ran into what I thought was a bug when adding months. After a bit of research, it appears that it wasn't a bug, but instead working as intended. According to the documentation found here:\[quote\] Example #2 Beware when adding or subtracting months\[/quote\]\[code\]<?php$date = new DateTime('2000-12-31');$date->modify('+1 month');echo $date->format('Y-m-d') . "\n";$date->modify('+1 month');echo $date->format('Y-m-d') . "\n";?>\[/code\]\[quote\]\[code\]The above example will output:2001-01-312001-03-03\[/code\]\[/quote\]Can anyone justify why this isn't considered a bug?Furthermore does anyone have any elegant solutions to correct the issue and make it so +1 month will work as expected instead of as intended?
 
Back
Top