簡體   English   中英

碳創造可讀的生日

[英]Carbon Create Readable Birthday

如何使用Laravel中的碳創建可讀的生日(如1990年1月1日)。

在我的User.php(模型)中,我有一個在數據庫中獲取生日列的函數

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

我怎么能從mysql數據庫中獲取生日並像1990年1月1日那樣返回它呢?或者像是在生日前1個月之類的東西。 :D

謝謝

可靠的方法:

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;
    }
}

您需要添加一些邏輯(打印日期,打印差異或其他內容),這取決於您如何在數據庫中存儲生日。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM