Add 2 months to date php

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


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.


asked May 14, 2012 at 15:43

Add 2 months to date php

morgimorgi

9853 gold badges16 silver badges24 bronze badges

2

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

echo date('d/m/Y', strtotime('+2 months'));

answered May 14, 2012 at 15:45

Alix AxelAlix Axel

148k91 gold badges390 silver badges493 bronze badges

1

Try using the DateTime object:

$date = new DateTime("+2 months");
echo $date->format("d/m/Y");

answered May 14, 2012 at 15:44

John CondeJohn Conde

215k98 gold badges447 silver badges489 bronze badges

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.

answered Jul 28, 2016 at 3:48

Add 2 months to date php

Fenix LamFenix Lam

3665 silver badges19 bronze badges

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');

answered Mar 11, 2018 at 14:45

Add 2 months to date php

SlavaSlava

2,8072 gold badges29 silver badges38 bronze badges

November 4, 2021 Category : PHP

Today, i would like to show you how to add months to date in php. you will learn how to add 6 months to current date in php. if you have question about php add months to date then i will give simple example with solution. We will use php add 1 month to current date.

I will give you very simple example how to add months to date in PHP. so let's see both example with output:

Example 1: PHP Add Months to Date

index.php

$date = "2021-11-01";

$newDate = date('Y-m-d', strtotime($date. ' + 3 months'));

echo $newDate;

?>

Output:

2022-02-01

Example 2: PHP Add Months to Current Date

index.php

$newDate = date('Y-m-d', strtotime(' + 4 months'));

echo $newDate;

?>

Output:

2022-03-01

i hope it can help you...

Add 2 months to date php

Hardik Savani

I'm a full-stack developer, entrepreneur and owner of Aatman Infotech. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.

Follow Me:

We are Recommending you

  • How to Extract All Email Addresses from String in PHP?
  • How to Get HTML Tag Value in PHP?
  • Laravel Carbon Get Yesterday Date Example
  • PHP Dropzone File Upload on Button Click Example
  • PHP Check If Array Is Multidimensional or Not
  • PHP - How to reindex array key after unset key?
  • Laravel File Upload with Progress Bar Example
  • How to find day from specific date in PHP?
  • How to remove null values from array in PHP?

Popular Posts

  • How to Convert String into Array in PHP?
  • How to use Union query with Laravel Eloquent?
  • How to add mysql trigger from migrations in Laravel 5?
  • How to fetch this week records in MySql ?
  • How to Get Previous Month from Date in PHP?
  • Laravel 5.7 Guzzle http client POST request example
  • Implement Flash Message with Laravel 5.7
  • PHP Subtract Month from Date Example
  • How to use sweet alert for delete confirm in Codeigniter?
  • How to upload image with validation in PHP Codeigniter?
  • How to create custom library file in codeigniter 3?

Categories

  • Laravel
  • Angular
  • PHP
  • jQuery
  • Bootstrap
  • Python
  • Javascript
  • MySql
  • Ajax
  • Node JS
  • HTML
  • Codeigniter
  • Ubuntu
  • Guest Post
  • Vue.JS
  • React JS
  • Git
  • Server
  • JSON
  • Installation
  • CSS
  • Google Map
  • SQL
  • .htaccess
  • Google API
  • JQuery UI
  • Axios
  • Typeahead JS
  • Socket.io
  • Highcharts

Latest Posts

  • How to Get Database Name in Laravel?
  • How to Check Database Connection in Laravel?
  • How to Get Request Header in Laravel?
  • Laravel - How to Get All Files in a Directory?
  • 13 High-Paying IT Jobs You Can Get Without a Degree

How to add months to date in php?

Example 1: PHP Add Months to Date.
index.php. $date = "2021-11-01"; $newDate = date('Y-m-d', strtotime($date. ' + 3 months')); ... .
Output: 2022-02-01..
index.php. $newDate = date('Y-m-d', strtotime(' + 4 months')); echo $newDate; ?>.
Output: Read Also: How to Add Days to Date in PHP? 2022-03-01. i hope it can help you....

How can I get plus date in PHP?

Method 2: Using date_add() Function: The date_add() function is used to add days, months, years, hours, minutes and seconds. Syntax: date_add(object, interval);

Which function can be used to change the date from current month to three months previous?

You can use the EDATE function to quickly add or subtract months from a date. The EDATE function requires two arguments: the start date and the number of months that you want to add or subtract. To subtract months, enter a negative number as the second argument. For example, =EDATE("9/15/19",-5) returns 4/15/19.