How to Get a Date One Month Later in PHP


How to Get a Date One Month Later in PHP

There are two ways to get a date one month later in PHP

  • By using the date function
  • By using the DateTime::modify

Illustration of the example using the date() function to get a date one month later in PHP

  1. We will use the date() function to get the date in a specific format.
  2. We will use the strtotime() function inside the date() to convert the date value in the timestamp.

Illustration of the example using the DateTime::modify

  1. We will use the DateTime() function to create the DateTime object from the given date.
  2. We will use the modify() function with the date object.

Complete Code:-

<?php
      $date1 = '2020-06-01';
      echo 'The given date is: '.$date1;
      echo '<br>';
//Now, we can get the date one month later from the above date
      echo 'The date after one month later is: '.date('Y-m-d',strtotime('+1 month',strtotime($date1)));
      echo '<br>';
//The above example will output: '2020-07-01';

//Alternative method using the datetime::modify
     $date2 = new DateTime('2020-06-01');
     $date2->modify('+1 month');
      echo $date2->format('Y-m-d');
//The above example will output: '2020-07-01';

	?>

Also Read, PHP difference between two dates in years, months and days

Conclusion:- I hope this tutorial will help you to understand. If you want to know more about the date function visit here.


2 thoughts on “How to Get a Date One Month Later in PHP”

  1. Attractive section of content. I just stumbled upon your site and
    in accession capital to assert that I acquire actually enjoyed account your blog posts.

    Any way I’ll be subscribing to your feeds and even I achievement you
    access consistently quickly.

    Reply

Leave a Comment