Can someone tell me how I can use the current year instead of hard-coding the year in the below line?

$yearCurrent = date('Y');
$dayLast = new DateTime('2014-12-31');

I guess this is probably something very simple but I couldn't find a solution for this so far.

有帮助吗?

解决方案 2

The last day of the year will always be 12-31 so try:

$dayLast = new DateTime($yearCurrent.'-12-31');

其他提示

why not just use:

$dayLast = $yearCurrent."-12-31";?

Or, more compactly:

$dayLast = new DateTime(date('Y').'-12-31');

Unless you need the variable set to current year for some other purpose.

Another variation, which will always give a date for the current year:-

$dayLast = \DateTime::createFromFormat('-m-d', '-12-31')->format('Y-m-d');

This does not require your $yearCurrent variable.

See the DateTime manual.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top