Get the last day of the current year as date

纵饮孤独 提交于 2020-01-03 08:05:14

问题


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.


回答1:


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'));



回答2:


You can just concatenate actual year with required date

$year = date('Y') . '-12-31';
echo $year;
//output 2014-12-31



回答3:


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');



回答4:


Another way to do this is use the Relative Formats of strtotime()

$yearEnd = date('Y-m-d', strtotime('last day of december this year'));

// or

$yearEnd = date('Y-m-d', strtotime('last day of december'));


来源:https://stackoverflow.com/questions/21976813/get-the-last-day-of-the-current-year-as-date

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!