Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the last day of the current year as date

How can I get the last day (Dec 31) of the current year as a date using PHP?

I tried the following but this doesn't work:

$year = date('Y');
$yearEnd =  strtotime($year . '-12-31');

What I need is a date that looks like 2014-12-31 for the current year.

like image 574
user2571510 Avatar asked Feb 24 '14 00:02

user2571510


3 Answers

You can just concatenate actual year with required date

$year = date('Y') . '-12-31';
echo $year;
//output 2014-12-31
like image 115
Fabio Avatar answered Nov 11 '22 14:11

Fabio


PHP strtotime() uses the current date/time as a basis (so it will use this current year), and you need date() to format:

$yearEnd = date('Y-m-d', strtotime('Dec 31'));

//or

$yearEnd = date('Y-m-d', strtotime('12/31'));
like image 20
AbraCadaver Avatar answered Nov 11 '22 12:11

AbraCadaver


DateTime is perfectly capable of doing this:

$endOfYear = new \DateTime('last day of December this year');

You can even combine it with more modifiers to get the end of next year:

$endOfNextYear = new \DateTime('last day of December this year +1 years');
like image 13
xtra Avatar answered Nov 11 '22 13:11

xtra