簡體   English   中英

laravel-碳| diffForHumans()較短的版本?

[英]laravel - Carbon | diffForHumans() shorter version?

我試圖弄清楚如何縮短diffForHumans中Carbon庫提供的diffForHumans方法的輸出。

diffForHumans的默認格式是這樣的:( 來自文檔

  • 將過去的值與現在的默認值進行比較時:

    1. 5個月前

    2. 1小時前

但我希望輸出是這樣的:

  1. 1小時
  2. 5分鍾
  3. 5個月
  4. 2年
  5. 現在

我該如何實現?

Carbon讓您可以選擇刪除“ ago”

$ time = \\ Carbon \\ Carbon :: now()-> subMinutes(1)-> diffForHumans(null,true)

在此處輸入圖片說明

如果您需要像“ 1小時5分鍾”這樣的用法,只需str_replace(['hours', 'minutes'], ['h', 'mins'], $time) ;

對於Just now ,您需要指定just now

按照diffForHumans的源代碼

/**
 * Get the difference in a human readable format in the current locale.
 *
 *
 * @param Carbon|null $other
 * @param bool        $absolute removes time difference modifiers ago, after, etc
 * @param bool        $short    displays short format of time units
 *
 * @return string
 */
public function diffForHumans(Carbon $other = null, $absolute = false, $short = false) {
    ...
}

刪除修飾符,將第二個參數傳遞為true並縮短時間,將第三個參數傳遞為true

源代碼位於

vendor/nesbot/carbon/src/Carbon/Carbon.php

暫無
暫無

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

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