简体   繁体   中英

Carbon Create Readable Birthday

how to create a readable birthday like January 1, 1990 using carbon in laravel.

In my User.php (Model) I have a function that get a birthday column in my database

public function getBirthdayAttribute($birthday)
{
    return $this->attributes['birthday'] = \Carbon\Carbon::????($birthday);
}

How can I get the birthday from mysql database and return it like January 1, 1990. Or something like 1 month to go before your birthday blah blah blah. :D

Thanks

The reliable way:

public function getBirthdayAttribute($birthday)
{
    Try {
      return \Carbon\Carbon::parse($birthday)->diffForHumans(); // 8 months ago / 1 month from now etc
      // or:
      // ->format('F j, Y'); // returns eg. January 1, 2000
    }
    catch (\Exception $e)
      return $birthday;
    }
}

You need to add some logic (print date, print diff or whatever), and it depends on how you store the birthday in the db.

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