• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • VBA
  • About
You are here: Home / PHP / Add Months to Date in php

Add Months to Date in php

April 27, 2022 Leave a Comment

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.

Other Articles You'll Also Like:

  • 1.  php cos – Find Cosine of Number in Radians Using php cos() Function
  • 2.  php array_splice() – Remove, Insert, and Replace Elements in Array
  • 3.  Get First Element of Array in php
  • 4.  php Random Number Generator with rand Function
  • 5.  How to Get Current System IP Address in php
  • 6.  php array_flip() – Flip the Keys and Values of an Array in php
  • 7.  php print_r() Function- Get Information about Variables in php
  • 8.  Remove First Character from String Using php
  • 9.  php floor – Round Down and Find the Floor of a Number in php
  • 10.  php Rename File – Renaming Files and Directories Easily

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

The Programming Expert is a compilation of hundreds of code snippets to help you find solutions to your problems in Python, JavaScript, PHP, HTML, SAS, and more.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2022 · The Programming Expert · About · Privacy Policy