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:
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!?
Select a cell(s) with dates, press Ctrl+1 to opent the Format Cells dialog. On the Number tab, select Custom and type either "mmm" or "mmmm" in the Type box to display abbreviated or full month names, respectively.
CHOOSE option Enter the month names you want to return (customized as you like) as values in CHOOSE, after the first argument, which is entered as MONTH(date). The MONTH function will extract a month number, and CHOOSE will use this number to return the "nth" value in the list.
It seems that there is no ready function for it, so I wrote it by myself. This should solve my problem. Thanks anyway for your answers and comments. If you will find some errors, please provide in the comments.
This function calculates the month and the year I will end in after adding some month. Then it checks if the date is right. If not, we have the case that the day is not in the target month, so we take the last day in the month instead. Tested with PHP 5.3.10.
<?php
$monthToAdd = 1;
$d1 = DateTime::createFromFormat('Y-m-d H:i:s', '2011-01-30 15:57:57');
$year = $d1->format('Y');
$month = $d1->format('n');
$day = $d1->format('d');
$year += floor($monthToAdd/12);
$monthToAdd = $monthToAdd%12;
$month += $monthToAdd;
if($month > 12) {
$year ++;
$month = $month % 12;
if($month === 0)
$month = 12;
}
if(!checkdate($month, $day, $year)) {
$d2 = DateTime::createFromFormat('Y-n-j', $year.'-'.$month.'-1');
$d2->modify('last day of');
}else {
$d2 = DateTime::createFromFormat('Y-n-d', $year.'-'.$month.'-'.$day);
}
$d2->setTime($d1->format('H'), $d1->format('i'), $d1->format('s'));
echo $d2->format('Y-m-d H:i:s');
You have several alternatives besides DateInterval.
Here are examples that use strtotime()
:
http://www.brightcherry.co.uk/scribbles/php-adding-and-subtracting-dates/
// Subtracting days from a date
$date = "1998-08-14";
$newdate = strtotime ( '-3 day' , strtotime ( $date ) ) ;
$newdate = date ( 'Y-m-j' , $newdate );
echo $newdate;
...
// Subtracting months from a date
$date = "1998-08-14";
$newdate = strtotime ( '-3 month' , strtotime ( $date ) ) ;
$newdate = date ( 'Y-m-j' , $newdate );
echo $newdate;
Here's are some links for DateInterval
:
http://php.net/manual/en/dateinterval.construct.php
What can go wrong when adding months with a DateInterval and DateTime::add?
Q: Exactly how do you define a "month"?
Def#1): a "month" is a "month" - regardless of #/days
Def#2): a "month" is 30 days (for example)
Def#3): a "month" is the #/days between the 1st Monday of subsequent months
etc. etc
Q: What exactly is your "definition"?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With