【问题标题】:Laravel carbon get today string [duplicate]Laravel carbon 获取今天的字符串 [重复]
【发布时间】:2017-12-17 13:32:00
【问题描述】:

如何在 Laravel 中使用 Carbon 获取字符串 "today, 12:26"
我有日期:"2017-12-17 12:26",我怎样才能得到这些格式?

1) 今天,12:26,
2) 1 小时前
3) 1 分钟前
3) 1 秒前
4) 昨天,12.26
5) 12 月 17 日 12:26

【问题讨论】:

  • 自己找出来应该不难。
  • strtotime 有很多问题,你可以把 strtotime 改成 carbon 并随意使用,1st,2nd

标签: php laravel php-carbon


【解决方案1】:

您应该查看 Carbon 中的 diffForHumans method。例如:

Carbon::now()->subDays(1)->diffForHumans()
//1 day ago

Carbon::now()->diffForHumans()
//1 second ago

Carbon::now()->subDays(25)->diffForHumans();
// 3 weeks ago

编辑:

您使用的是 Laravel,请注意您也可以像这样调用 diffForHumans

$user->created_at->diffForHumans();

【讨论】:

    【解决方案2】:

    Carbon 中也有类似的东西,->diffForHumans(); 方法。示例:

    echo Carbon::now()->subDays(1)->diffForHumans();
    

    生产:

    1 天前

    您可以随时参考 Carbon 文档,文档非常详细。 Carbon Documentation.

    【讨论】:

      猜你喜欢
      • 2017-12-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-03
      • 2020-12-31
      • 1970-01-01
      • 2020-11-13
      • 2019-11-29
      相关资源
      最近更新 更多