Carbon 是继承自 PHP DateTime 类 的子类,但比后者提供了更加丰富、更加语义化的 API。其中一个比较实用的 API 就是 diffForHumans 方法,几乎每个用 Laravel 构建的项目中都有用到它。
比如,一个博客系统里的文章发布时间,显示格式可能就像下面这样:
**距离现在时间**      **显示格式**
< 1小时 xx分钟前
1小时 - 24小时 xx小时前
1天 - 15天 xx天前
> 15天 直接显示日期
这种显示方式非常人性化,在 Laravel 中设置它也很方便。
第一步:本地化 Carbon。在 AppServiceProvider 的 boot 方法中添加 Carbon::setLocale('zh')。
use Carbon\Carbon;
public function boot() {
Carbon::setLocale('zh');
}
繁体中文的设置是 Carbon::setLocale('zh-TW'),语言配置文件可在 vendor/nesbot/carbon/src/Carbon/Lang 文件夹下找到。
第二步:在 Model 中设定要人性化显示的字段。以 Article Model 的 created_at 字段为例。 use Carbon\Carbon;
public function getCreatedAtAttribute($date) {
if (Carbon::now() > Carbon::parse($date)->addDays(15)) {
return Carbon::parse($date);
} return Carbon::parse($date)->diffForHumans();
}
下面就可以直接使用了。
$article->created_at; // 1秒前

Carbon 的 diffForHumans 方法的更多相关文章

  1. Laravel 中设置 Carbon 的 diffForHumans 方法返回中文

    在写 feed 流功能时,经常要用到 Carbon 的 diffForHumans 方法,以方便返回直观的时间描述. 例如 Carbon::parse($date)->diffForHumans ...

  2. laravel框架少见方法详解

    1.whereDate() 方法 $q->where('created_at', '>=', date('Y-m-d').' 00:00:00')); 以前查数据时,直接用where条件来 ...

  3. Laravel 日期时间处理包 Carbon 的应用

    在编写 PHP 应用时经常需要处理日期和时间,这篇文章带你了解一下 Carbon – 继承自 PHP DateTime 类的 API 扩展,它使得处理日期和时间更加简单.Laravel 中默认使用的时 ...

  4. Carbon中文使用手册

    Introduction Carbon 继承了PHP的 Datetime 类和JsonSerialiable.所以 Carbon 中没有涉及到的,但在 Datetime 和JsonSerializab ...

  5. Carbon document

    <   Getting Started Docs Reference History Contribute Github Introduction The Carbon class is inh ...

  6. Laravel Carbon 简明使用

    快速切換前後日期 <?php use Carbon\Carbon; $now = Carbon::now(); echo $now; // 2015-03-26 00:36:47 $today ...

  7. laravel Carbon函数

    原文地址:https://blog.csdn.net/lbwo001/article/details/53063867 carbon官方网站:https://carbon.nesbot.com/doc ...

  8. laravel开发扩展记录

    whoops 错误提示扩展 whoops 是一个非常优秀的 PHP Debug 扩展,它能够使你在开发中快速定位出错的位置.laravel默认安装.区域 1 -- 是错误异常的简介区域 2 -- 是错 ...

  9. Laravel 5.1 文档攻略 —— Eloquent: 读取器和修饰器

    date_range 8月前 tag_faces Woody remove_red_eye 1483 chat0 简介 这一章其实很简单,Model的属性不是和数据表的字段一一对应吗? 那么在存储和呈 ...

随机推荐

  1. day5——Java 实现导出excel表 POI(转)

    1.首先下载poi-3.6-20091214.jar,下载地址如下: http://download.csdn.net/detail/evangel_z/3895051 2.Student.java ...

  2. ES6学习笔记(五):Class和Module

    Class Class 只是一个语法糖,其功能用es5也能实现,但是比es5更符合类的期待 定义: constructor代表构造方法,而this指向new 生成的实例 定义类方法时,可以不使用fun ...

  3. kvm安装配置

    操作系统版本:CentOS Linux release 7.5.1804 (Core) 内核版本:3.10.0-862.el7.x86_64 1.安装 安装以下软件.yum -y install qe ...

  4. SpringBoot设置事务隔离等级

    "If you're gonna play the game, boy, ya gotta learn to play it right" Spring Boot 使用事务非常简单 ...

  5. oracle函数验证时间格式并返回

    CREATE OR REPLACE FUNCTION WSW(parameter VARCHAR2) RETURN DATE IS val DATE; BEGIN IF (REGEXP_INSTR(p ...

  6. Fib的奇怪定理 : gcd(F[n],F[m])=F[gcd(n,m)]

    引理1:gcd(F[n],f[n-1])=1 因为 F[n]=f[n-1]+F[n-2] 所以 gcd(F[n],f[n-1]) = gcd(F[n-1]+F[n-2],F[n-1]) gcd的更损相 ...

  7. root和alias的区别

    先来看看官方说明: root 的用法: location /request_path/image/ { root /local_path/; } 当客户端请求 /request_path/image/ ...

  8. Python 文件解压缩

    shutil对压缩包的处理是通过调用zipFile和tarFile两个模块来进行的. import zipfile # zipfile压缩 z = zipfile.ZipFile('ss.zip', ...

  9. 关于swiper的tab(选项卡)中设置了autoHeight没有效果解决

    autoHeight属性使用看官网的示例:https://www.swiper.com.cn/api/parameters/294.html swiper的选项卡结构查看:https://www.sw ...

  10. mybatis+sqlserver中返回非自增主键

    首先把实体类贴出来(这里只贴出属性,其它的就是getter和setter方法): public class Around {     private String xccd;  //对应主键      ...