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. MySQL基础(一):基本操作

    一.下载安装及连接 MySQL是一个关系型数据库管理系统,由瑞典MySQL AB 公司开发,目前属于 Oracle 旗下公司.MySQL 最流行的关系型数据库管理系统,在 WEB 应用方面MySQL是 ...

  2. robotframework+Selenium2Library 模态窗口的处理

    原文链接:https://www.cnblogs.com/zuola/p/5750018.html   所谓模态窗口,就是指除非采取有效的关闭手段,用户的鼠标焦点或者输入光标将一直停留在其上的对话框. ...

  3. 获取androdmanifest里面的meta-data

    /* * Copyright 2017 JessYan * * Licensed under the Apache License, Version 2.0 (the "License&qu ...

  4. oracle系统表v$session、v$sql字段说明

    在本视图中,每一个连接到数据库实例中的 session都拥有一条记录.包括用户 session及后台进程如 DBWR, LGWR, arcchiver等等. V$SESSION中的常用列 V$SESS ...

  5. 将句子表示为向量(下):基于监督学习的句子表示学习(sentence embedding)

    1. 引言 上一篇介绍了如何用无监督方法来训练sentence embedding,本文将介绍如何利用监督学习训练句子编码器从而获取sentence embedding,包括利用释义数据库PPDB.自 ...

  6. 将句子表示为向量(上):无监督句子表示学习(sentence embedding)

    1. 引言 word embedding技术如word2vec,glove等已经广泛应用于NLP,极大地推动了NLP的发展.既然词可以embedding,句子也应该可以(其实,万物皆可embeddin ...

  7. bzoj千题计划264:bzoj3884: 上帝与集合的正确用法

    http://www.lydsy.com/JudgeOnline/problem.php?id=3884 欧拉降幂公式 #include<cmath> #include<cstdio ...

  8. Nginx配置项优化(转载)

    (1)nginx运行工作进程个数,一般设置cpu的核心或者核心数x2 如果不了解cpu的核数,可以top命令之后按1看出来,也可以查看/proc/cpuinfo文件 grep ^processor / ...

  9. SpringMvc数据校验@Valid等注解的使用与工具类抽取

    最近在重构老项目的代码,发现校验入参占用了很多代码,之前我对这一块的认识局限于使用StringUtils等工具来多个if块进行判断,代码是没什么问题,但是总写这些令人生烦,毕竟写代码也要讲究优雅的嘛, ...

  10. phpStorm 8.0.3 设置

    phpstorm 8 license key Learn Programming===== LICENSE BEGIN =====63758-1204201000000Ryqh0NCC73lpRm!X ...