Current date + 2 months
Asked Answered
F

4

28

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;
?>
Frazer answered 14/5, 2012 at 15:43 Comment(2)
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.Crabber
Proper return value checking does help as well.Stratovision
T
47

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

echo date('d/m/Y', strtotime('+2 months'));
Twinned answered 14/5, 2012 at 15:45 Comment(1)
Be extra careful when using strotime. On July 31st, date('dmY') yields "31072014" while date('dmY', strotime('-1 month')) yields "01072014" (I expected "30062014").Spit
B
14

Try using the DateTime object:

$date = new DateTime("+2 months");
echo $date->format("d/m/Y");
Bigod answered 14/5, 2012 at 15:44 Comment(4)
Correct, but not relevant to the case.Fraternize
Why is this answer not relevant? Just curious @StanislavShabalinChoirboy
@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.Fraternize
@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 provideOx
A
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.

Antonantone answered 28/7, 2016 at 3:48 Comment(0)
T
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');
Tricotine answered 11/3, 2018 at 14:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.