获取当年的最后一天作为日期

use*_*510 8 php datetime datetime-format date-arithmetic

如何使用PHP将当前年的最后一天(12月31日)作为日期?

我尝试了以下但这不起作用:

$year = date('Y');
$yearEnd =  strtotime($year . '-12-31');
Run Code Online (Sandbox Code Playgroud)

我需要的是今年的2014-12-31日期.

Abr*_*ver 16

PHP strtotime()使用当前日期/时间作为基础(因此它将使用当前年份),您需要date()格式化:

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

//or

$yearEnd = date('Y-m-d', strtotime('12/31'));
Run Code Online (Sandbox Code Playgroud)


Fab*_*bio 10

您可以将实际年份与所需日期连接起来

$year = date('Y') . '-12-31';
echo $year;
//output 2014-12-31
Run Code Online (Sandbox Code Playgroud)

  • ......甚至是'日期('Y-12-31')` (9认同)

xtr*_*tra 5

DateTime完全有能力做到这一点:

$endOfYear = new \DateTime('last day of December this year');
Run Code Online (Sandbox Code Playgroud)

您甚至可以将其与更多修饰符结合使用,直到明年年底:

$endOfNextYear = new \DateTime('last day of December this year +1 years');
Run Code Online (Sandbox Code Playgroud)