Possible Duplicate:
PHP DateTime::modify adding and subtracting months
I have a starting date (i.e. 2011-01-30) and want to add 1 month.
The problem is in defining what a month is. So if I use the following code:
$d1 = DateTime::createFromFormat('Y-m-d H:i:s', '2011-01-30 15:57:57');
$d1->add(new DateInterval('P1M'));
echo $d1->format('Y-m-d H:i:s');
I get the following result: 2011-03-02 15:57:57
The problem is, that I need it to use the following rules:
- If I add 1 month it will just add 1 on the month part and leave the day part (2011-01-15 will become 2011-02-15)
- If the day is not existing in the month we will end, we take the last existing day of it (2011-01-30 will become 2011-02-28)
Is there a common function in php that can do this or do I have to code it by myself? Maybe I'm just missing a parameter or something!?