Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add 1 month on a date without skipping i.e. february [duplicate]

Tags:

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!?

like image 667
Kasihasi Avatar asked May 23 '12 16:05

Kasihasi


People also ask

How to format month in Excel?

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.

How to get month name in Excel formula?

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.


2 Answers

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');
like image 198
Kasihasi Avatar answered Oct 07 '22 21:10

Kasihasi


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"?

like image 30
paulsm4 Avatar answered Oct 07 '22 19:10

paulsm4