In this tutorial, we are going to show you how to get next year’s date using PHP.
For example, if today’s date is “May 28th, 2022”, then we want to be able to find “May 28th, 2023”.
To do this, we can use the DateTime object.
Take a look at the following example:
//Instantiate the DateTime object. $now = new DateTime(); //You can get next year's date by using the //DateTime::add function and DateInterval $nextYearDT = $now->add(new DateInterval('P1Y')); //Retrive the date in a YYYY-MM-DD format. $nextYear = $nextYearDT->format('Y-m-d'); //var_dump the result. var_dump($nextYear);
In the PHP code above:
- We created a new DateTime object.
- Afterwards, we provided the DateTime::add function with a DateInterval of P1Y. P1Y means “a period of one year”. If we wanted to get a date that is 5 years in the future, we would have used P5Y as the interval instead.
- Finally, we retrieved the date in a “YYYY-MM-DD” format and then printed out the result.
When we ran this example on December 14th, 2019, the result was 2020-12-14, which is correct.
If you just want next year without the month or the day, then you can simply remove the “m” and “d” format characters:
//Get the date in a YYYY format. $nextYear = $nextYearDT->format('Y');
This will result in “2023”.
If you want to get next year’s date using PHP’s date and strtotime functions, then you can try the following example:
//Using date and the strtotime function $nextYear = date("Y-m-d", strtotime("+1 year")); var_dump($nextYear);
As you can see, we were able to pass in “+1 year” as a parameter to PHP’s strtotime function.
The result of strtotime provides the date function with a Unix timestamp of next year’s date.
Related articles that you might find helpful: