문제

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