热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

使用碳来返回人类可读的datetime差异。-UsingCarbontoreturnahumanreadabledatetimedifference

ImusingLaravel4tocreatemyproject.我正在使用Laravel4来创建我的项目。Iamcurrentlybuildingthecommen

I'm using Laravel 4 to create my project.

我正在使用Laravel 4来创建我的项目。

I am currently building the comments section and I want to display how long ago the post was created, kind of like Facebook's '10 mins ago' & '2 weeks ago' etc.

我现在正在构建评论部分,我想展示一下这个帖子是多久之前创建的,有点像Facebook的“10分钟前”和“两周前”等等。

I have done a little bit of research and found that a package called Carbon can do this.

我做了一些研究,发现一个叫做碳的包裹可以做到这一点。

After reading the Laravel doc's, it says:

读了拉拉维尔医生的书后,上面写着:

By default, Eloquent will convert the created_at, updated_at, and deleted_at columns to instances of Carbon, which provides an assortment of helpful methods, and extends the native PHP DateTime class.

默认情况下,strate将created_at、updated_at和deleted_at列转换为Carbon实例,这提供了各种有用的方法,并扩展了本机PHP DateTime类。

But when I return a date column that I have created, it doesn't display it like on Facebook.

但当我返回我创建的日期列时,它不会像在Facebook上那样显示它。

The code that I'm using is:

我使用的代码是:

return array('time');

Has any body used this Carbon package that could give me a hand in doing what I need, I'm quite confused.

有没有人用过这个碳包可以帮助我做我需要的事,我很困惑。

5 个解决方案

#1


61  

If you read the Carbon docs to get what you want you call the diffForHumans() method.

如果您阅读碳文档以获得您想要的内容,您可以将其称为diffforhuman()方法。

created_at))->diffForHumans() ?>

#2


99  

By default, Eloquent will convert the created_at, updated_at, and deleted_at columns to instances of Carbon. So, your code should be just like this:

默认情况下,有说服力的将转换created_at、updated_at和deleted_at列到碳的实例。所以,你的代码应该是这样的:

$comment->created_at->diffForHumans();

It's very cool. It'll produce string like 2 minutes ago or 1 day ago. Plurar or singular, seconds, minutes, hours, days, weeks, or years, it runs automatically. I've tested it on Laravel version 4.1.24.

这是非常酷的。它会在2分钟前或1天前产生弦。复数或单数,秒,分,小时,天,周,年,它自动运行。我已经在Laravel的4.1.24版上测试过了。

#3


2  

use this code for time ago :

使用此代码的时间以前:

public function time_elapsed_string($datetime, $full = false) {
$now = new DateTime;
$ago = new DateTime($datetime);
$diff = $now->diff($ago);

$diff->w = floor($diff->d / 7);
$diff->d -= $diff->w * 7;

$string = array(
     'y' => 'year',
     'm' => 'month',
     'w' => 'week',
     'd' => 'day',
     'h' => 'hour',
     'i' => 'minute',
     's' => 'second',
 );
 foreach ($string as $k => &$v) {
     if ($diff->$k) {
         $v = $diff->$k . ' ' . $v . ($diff->$k > 1 ? 's' : '');
     } else {
         unset($string[$k]);
     }
 }

 if (!$full) $string = array_slice($string, 0, 1);
 return $string ? implode(', ', $string) . ' ago' : 'just now';

}

}

#4


2  

For any version of Laravel

任何版本的拉拉维尔

$message->updated_at->diffForHumans();

#5


0  

Carbon::parse($p->created_at)->diffForHumans();

碳:解析($ p - > created_at)- > diffForHumans();


推荐阅读
author-avatar
平凡2188
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有