Laravel(Carbon)仅以天为单位显示日期差 [英] Laravel (Carbon) display date difference only in days

查看:75
本文介绍了Laravel(Carbon)仅以天为单位显示日期差的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何仅按天显示差异. 在这里$price->created_at = 2014-04-28

How to display diffrence only by days. Here $price->created_at = 2014-04-28

\Carbon\Carbon::createFromTimeStamp(strtotime($price->created_at))->diffForHumans()

谢谢!

推荐答案

假设您希望与now()有所区别,并且diffForHumans的结果适合您,但今天除外:

Suppose you want difference to now() and result from diffForHumans suits you except for today:

$created = new Carbon($price->created_at);
$now = Carbon::now();
$difference = ($created->diff($now)->days < 1)
    ? 'today'
    : $created->diffForHumans($now);

无需两次调用Carbon :: now(),因此请使用$ now.

edit: no need to call Carbon::now() twice so use $now instead.

这篇关于Laravel(Carbon)仅以天为单位显示日期差的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆