To add months to a date in php, you can use the strtotime() function as shown below.

$today = "2022-04-27";

$one_month_in_future = strtotime($today . "+1 month");

echo date('Y-m-d', $one_month_in_future);

//Output: 
2022-05-27

You can also use the php date_add() function.

$today= date_create("2022-04-27");

$one_month_in_future = date_add($today, date_interval_create_from_date_string("1 month"));

echo date_format($one_month_in_future, "Y-m-d");

// Output:
2022-05-27

When working with dates in php, the ability to easily change, add time to, or subtract time from a date is valuable.

One situation which is common is when you need to add months to a date.

There are a few ways you can add months to a date in php. The easiest is with the php strtotime() function.

strtotime() allows us to work with textual date-time descriptions and convert them to a UNIX timestamps.

Below is how you can use strtotime() in php to add a month to a date.

$today = "2022-04-27";

$one_month_in_future = strtotime($today . "+1 month");

echo date('Y-m-d', $one_month_in_future);

//Output: 
2022-05-27

Using the php date_add() Function to Add Months to a Date in php

Another method you can use to add periods of time to a date is with the php date_add() function.

date_add() adds a specified DateInterval object to a specified DateTime object created from date_create().

Below is a simple example of how you can add a month to a date with date_add().

$today= date_create("2022-04-27");

$one_month_in_future = date_add($today, date_interval_create_from_date_string("1 month"));

echo date_format($one_month_in_future, "Y-m-d");

// Output:
2022-05-27

Hopefully this article has been useful for you to learn how to add months to a date in php.

Categorized in:

PHP,

Last Update: March 13, 2024