-1

Just a noob question,

For example I have date 2013-08-01 // 1 August 2013 How do I get same day next month. ex. 2013-09-01 // 1 Sept 2013

Thanks

Jason
  • 33
  • 7

4 Answers4

1

I'm not a PHP dev, but it seems to me you want DateTime::add

$date->add(new DateInterval('P1M'));

As noted in the documentation though, you need to be careful near the end of a month - adding 1 month to January 31st 2001 ends up with March 3rd, for example. (Not the behaviour I'd choose, but that's a different matter.) If you're always dealing with the 1st of the month, you should be okay.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Please not in OOP way – Jason Aug 06 '13 at 06:44
  • $sDate = date('Y-m-d', strtotime($sDate.' + 1 month ')); – Alma Do Aug 06 '13 at 06:45
  • @Jason The [manual](http://www.php.net/manual/en/datetime.add.php) gives examples both ways. – devnull Aug 06 '13 at 06:45
  • Apart from having hundreds of answers showing how to add n hours/day/months/whatever to a date, this will *not* give the same day on the next month due to months not being fixed size. – Gordon Aug 06 '13 at 06:46
  • @Gordon: It will on the example given, surely - there's *always* a 1st of the month. I would expect this only to give problems for days near the end of the month. (I'll add a note for that.) – Jon Skeet Aug 06 '13 at 06:51
  • Reason for the behavior is outlined in http://www.gnu.org/software/tar/manual/html_node/Relative-items-in-date-strings.html#SEC120 – Gordon Aug 06 '13 at 06:57
  • @Gordon: Still seems broken to me :) It makes more sense (IMO) to return the end of the month. – Jon Skeet Aug 06 '13 at 07:01
  • Well, we had tons of "bug reports" about this behavior over the years so in retrospect you might be right on that. – Gordon Aug 06 '13 at 07:22
0

With using strtotime

echo date("Y-m-d",strtotime('next month',strtotime('2013-08-01')));
Bora
  • 10,529
  • 5
  • 43
  • 73
0

You can use the date_add function (procedural) or Datetime::add (OO). Here is an example of each:

$date = new DateTime('2000-01-01');
$date->add(new DateInterval::createFromDateString('1 month'));

or

$date = date_create('2000-01-01');
date_add($date, date_interval_create_from_date_string('1 month'));
Keynir
  • 1
0

Try this:

$dateTest = date('Y-m-d', strtotime("$dateTest + 1 month"));