27

I wrote this piece of code in order to display the current date + 2 months :

<?php
    $date = date("d/m/Y");
    $date = strtotime(date("d/m/Y", strtotime($date)) . "+2 months");
    $date = date("d/m/Y",$date);
    echo $date;
?>

It does not seem to work as it displays : 01/03/1970.

What am I doing wrong?

Thanks for your help.

EDIT :

After reading comments and answers, I simplified and corrected it.

<?php
    $date = date("d/m/Y", strtotime(" +2 months"));
    echo $date;
?>
morgi
  • 1,005
  • 3
  • 17
  • 24
  • Try putting a space before your `+2`. And maybe use two variables - using the same variable name over and over for different types of data is confusing and bad practice. – WWW May 14 '12 at 15:46
  • Proper return value checking does help as well. – hakre May 18 '12 at 12:34

4 Answers4

46

You're missing the second argument for the second strtotime() call:

echo date('d/m/Y', strtotime('+2 months'));
Alix Axel
  • 151,645
  • 95
  • 393
  • 500
  • 5
    Be extra careful when using strotime. On July 31st, date('dmY') yields "31072014" while date('dmY', strotime('-1 month')) yields "01072014" (I expected "30062014"). – Paul Voss Aug 05 '14 at 15:50
14

Try using the DateTime object:

$date = new DateTime("+2 months");
echo $date->format("d/m/Y");
John Conde
  • 217,595
  • 99
  • 455
  • 496
  • 1
    Correct, but not relevant to the case. – Stanislav Shabalin May 14 '12 at 15:46
  • Why is this answer not relevant? Just curious @StanislavShabalin – Muhammad Ibnuh Aug 08 '17 at 14:11
  • @MuhammadIbnuh To be honest, I can't remember or figure it out five years later :–) Maybe because why use `DateTime` when you already have `strtotime` and it's fine to just fix a typo as in the accepted answer. – Stanislav Shabalin Aug 10 '17 at 14:54
  • @StanislavShabalin, `strtotime` is not giving proper answer, try this, if your current date is Feb 01, 2019 then add 2 months and try... It gives March 02, 2019. It needs to be Apr 01, 2019. is there any solution bro...? kindly provide – Shurvir Mori Dec 19 '19 at 19:24
4

If today is "YYYY-mm-31" and next month does not have the 31th day, it will show the next month of that day, make the system display "+3 months" result instead of "+2 months" result.

So I guess this is the most safety:

$end_date=date("Y-m-d",strtotime("+2 month",strtotime(date("Y-m-01",strtotime("now") ) )));

Change the date to the 1st day first.

Fenix Lam
  • 386
  • 6
  • 22
4

Using DateTime->add() or DateTime->modify()

If you are working with an existing DateTime object, you can use one of these:

// Your date
$date = new DateTime(); // empty for now or pass any date string as param

// Adding
$date->add(new DateInterval('P2M')); // where P2M means "plus 2 months"

// or even easier
$date->modify('+2 months');

// Checking
echo $date->format('Y-m-d');

Beware of adding months in PHP, it may overflow to the next month if the day in the original date is higher than the total number of days in the new month. Same overflow happens with leap years when adding years. Somehow this is not considered a bug by PHP developers and is just documented without a solution. More here: PHP DateTime::modify adding and subtracting months

I found this to be the most to-the-point solution to address overflow problem:

$day = $date->format('j');
$date->modify('first day of +2 months')->modify('+'. (min($day, $date->format('t')) - 1) .' days');
Slava
  • 2,887
  • 3
  • 30
  • 39