Question

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.

Was it helpful?

Solution 2

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

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

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top