【问题标题】:Carbon diffForHumans with monthsCarbon diffForHumans 与几个月
【发布时间】:2017-12-11 15:01:13
【问题描述】:

我尝试使用 Carbon 在 Laravel 应用程序中获取两个日期之间的差异,即 date now() 和给定日期之间的差异。我在刀片中执行以下操作

\Carbon\Carbon::parse(Auth::user()->properties->first()->von)->diffForHumans()

但这仅返回年份,因此以下面的示例为例,其中给定日期是1st September, 2011,我得到的结果是 6 年前预期为 6 年零 3 个月。 有可能用 Carbon 实现这一目标吗?

【问题讨论】:

标签: php laravel php-carbon


【解决方案1】:

您可以使用diff() 及其属性:

$diff = auth()->user()->properties->first()->von->diff(now());

然后显示出来:

The difference is {{ $diff->y }} years and {{ $diff->m }} months

或者,您可以在几个月内获得差异并使用它:

$diffInMonths = auth()->user()->properties->first()->von->diffInMonths(now());

【讨论】:

  • 1 年 6 个月 1 天前
  • 1 十年 1 年 6 个月前
  • 4 个月 3 周 2 天后
  • 我仍在寻找类似上面的差异
猜你喜欢
  • 1970-01-01
  • 2017-10-21
  • 1970-01-01
  • 2018-08-11
  • 1970-01-01
  • 2017-04-01
  • 1970-01-01
  • 2018-07-14
  • 2017-05-19
相关资源
最近更新 更多