简体   繁体   中英

PHP Carbon change date to current year (Laravel)

Is there a simple way using carbon to change a date string to the current year? I need it to display a birthday in my calendar. I currently am doing the following in my blade view:

{{ \Carbon\Carbon::createFromDate('Y-m-d', $contact->birthdate)->format('Y-m-d') }}

The question is, how can I change the year to the current year? I have been playing around with this from the docs:

$xmasThisYear = Carbon::createFromDate(null, 12, 25);  // Year defaults to current year

But I do not know how to change the year from an existing date string. The following for example does not work:

{{ \Carbon\Carbon::createFromDate('m-d', $contact->birthdate)->format('Y-m-d') }}

Any pointers will be greatly appreciated!

After posting the question I figured it out, I can simply do the following:

{{ \Carbon\Carbon::parse($contact->birthdate)->year(now()->format('Y'))->format('Y-m-d') }}

->year(now() simply changes the year to the current year.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM