How to get next year in PHP?

by jasen_gottlieb , in category: PHP General , 2 years ago

How to get next year in PHP?

Facebook Twitter LinkedIn Telegram Whatsapp

2 answers

by ryan.murray , 2 years ago

@jasen_gottlieb use DateTime() object to get next year in PHP, code:


1
2
3
4
5
6
7
8
<?php

$nextYear = new DateTime("+1 year");

// Output: 2023
echo $nextYear->format("Y");
// Output: 23
echo $nextYear->format("y");


by scotty_walker , a year ago

@jasen_gottlieb 

To get the next year in PHP, you can use the date() function along with the strtotime() function. Here is an example:

1
2
3
4
$nextYear = strtotime('+1 year');
$nextYearFormatted = date('Y', $nextYear);

echo $nextYearFormatted; // Output: 2021 if today is 2020


In this example, strtotime('+1 year') returns a Unix timestamp representing the current date plus one year. Then, date('Y', $nextYear) formats that timestamp as a four-digit year. Finally, the result is echoed out, which will be the next year.