亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在 Laravel 中的數據庫結果上附加一列

在 Laravel 中的數據庫結果上附加一列

PHP
江戶川亂折騰 2023-07-01 09:38:51
我在 Laravel 應用程序中返回一些結果,我想在返回的結果中添加一個友好的時間列,而不是顯示時間戳。我想要做的是,如果時間戳是今天,則顯示保存的時間,即 09:45,如果昨天根據今天的日期創建,那么我想顯示“昨天”,如果它比昨天舊,那么我想要顯示日期“12/6/2020”。我只是無法使用 Carbon 計算出邏輯,這是到目前為止我的邏輯,protected $appends = ['friendly_date'];public function getFriendlyDateAttribute(){    if($this->created_at < Carbon::now()->subDays("1") {        return "Yesterday";    }}
查看完整描述

1 回答

?
四季花海

TA貢獻1811條經驗 獲得超5個贊

Carbon 提供了一個實用函數來做到這一點:


protected $appends = ['friendly_date'];


public function getFriendlyDateAttribute()

{

    if ($this->created_at->isToday()) {

        return $this->created_at->format('H:i');

    }

    if($this->created_at->isYesterday()) {

        return "Yesterday";

    }

    return $this->created_at->format('d/m/y'); // Assuming this is what 12/6/2020 means

}


查看完整回答
反對 回復 2023-07-01
  • 1 回答
  • 0 關注
  • 143 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號