php 使用 Carbon 返回人类可读的日期时间差异
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17260231/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using Carbon to return a human readable datetime difference
提问by BigJobbies
I'm using Laravel 4to 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 分钟前”和“2 周前”等。
I have done a little bit of research and found that a package called Carboncan do this.
我做了一些研究,发现一个叫做Carbon的包可以做到这一点。
After reading the Laravel doc's, it says:
阅读 Laravel 文档后,它说:
By default, Eloquent will convert the
created_at
,updated_at
, anddeleted_at
columns to instances ofCarbon
, which provides an assortment of helpful methods, and extends the native PHPDateTime
class.
默认情况下,机锋将转换
created_at
,updated_at
和deleted_at
列的情况下Carbon
,它提供了有用的方法的分类,并扩展了原生的PHPDateTime
类。
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.
有没有人使用过这个碳包,可以帮我做我需要的事情,我很困惑。
回答by Ifan Iqbal
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:
默认情况下,Eloquent 会将 created_at、updated_at 和 deleted_at 列转换为 Carbon 的实例。所以,你的代码应该是这样的:
$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 minutes ago
或这样的字符串1 day ago
。复数或单数,秒,分,小时,天,周或年,它会自动运行。我已经在 Laravel 4.1.24 版上对其进行了测试。
回答by Altrim
If you read the Carbon docs to get what you want you call the diffForHumans()
method.
如果您阅读 Carbon 文档以获取您想要的diffForHumans()
方法,则可以调用该方法。
<?php echo \Carbon\Carbon::createFromTimeStamp(strtotime($comment->created_at))->diffForHumans() ?>
回答by Rahul Hirve
For any version of Laravel
适用于任何版本的 Laravel
$message->updated_at->diffForHumans();
回答by fdyahk
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';
}
回答by Hari Pudyal
Carbon::parse($p->created_at)->diffForHumans();