PHP DateTime on the first day of next year

I wonder if there is a “right” way to call:

$endDate = new DateTime('first day of next year');

I know that the PHP parser expects a month later of( see here ), but next yearit seems to work as intended. If the month is May, he will declare April 1 of next year how $endDate.

Of course, we can hack this with:

$endDate = new DateTime('first day of january');
$endDate = $endDate->modify('+1 year');

But I can not imagine that no one fixed this error, since PHP 5.3 came out 7 years ago.

+4
source share
1 answer

This is not as straightforward as you could look for (are there situations where January is not the first month?), But it works great for PHP 5.2

$endDate = new DateTime('1st January Next Year');
echo $endDate->format('Y-m-d'); // 2017-01-01

https://3v4l.org/H5P8s

+6
source

All Articles