Laravel Carbon DiffForHumans 选项(年、月、日)

问题描述 投票:0回答:1

我已经阅读了

DiffForHumans
的文档,非常简单,但是有没有办法自定义打印日期:

  • 13年1个月10天
  • 01年11月01日

它将尊重年/年、月/月、日/日?

  • 方法一:

    $date = Carbon::parse('2010-08-29');
    $formattedDate = $date->diffForHumans(['parts' => 3, 'join' => ', '];
    

    将打印出来

    13 years, 1 month, 4 weeks ago

  • 方法2:

    $date = Carbon::parse('2010-08-29');
    $formattedDate = $date->diff(now())->format('%y Year, %m Months and %d Days')
    

    将打印出来

    13 Year, 1 Months and 28 Days

  • 方法 3(备份选项):

    $date = Carbon::parse('2010-08-29');
    $formattedDate = $date->diff(now())->format('%y Year(s), %m Month(s) and %d Day(s)')
    

    将打印出来

    13 Year(s), 1 Month(s) and 28 Day(s)

是否有一个普通的 PHP 类可以完成类似的事情?

php laravel php-carbon
1个回答
0
投票

您可以跳过您不想要的单元:

$date = Carbon::parse('2010-08-29');
$formattedDate = $date->diffForHumans([
    'parts' => 3,
    'join' => ', ',
    'skip' => 'week',
]);
© www.soinside.com 2019 - 2024. All rights reserved.