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
- We will use the date() function to get the date in a specific format.
- 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
- We will use the DateTime() function to create the DateTime object from the given date.
- 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.
Hi there, I log on to your new stuff daily. Your writing style is awesome,
keep up the good work!
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.