Php 如何在laravel 4中发布(时间)

Php 如何在laravel 4中发布(时间),php,datetime,laravel,laravel-4,Php,Datetime,Laravel,Laravel 4,我正在用Laravel 4制作我的网站,我在表中的创建了和更新了字段。我想制作一个新闻系统,它能告诉我这篇文章发表以来已经过去了多少时间 | name | text | created_at | updated_at | | __________ | __________ | ________________________ | ___________________ | | news name | news_text |

我正在用Laravel 4制作我的网站,我在表中的
创建了
更新了
字段。我想制作一个新闻系统,它能告诉我这篇文章发表以来已经过去了多少时间

|    name    |    text    |        created_at        |      updated_at     |
| __________ | __________ | ________________________ | ___________________ |
| news name  | news_text  |   2013-06-12 11:53:25    | 2013-06-12 11:53:25 |
我想展示一些东西,比如:

-创建于5分钟前

-创建于5个月前

如果职位超过1个月

-创建于2012年11月5日

尝试使用。Laravel已经将其作为一个依赖项提供,因此没有必要将其添加到您的依赖项中

use Carbon\Carbon;

// ...

// If more than a month has passed, use the formatted date string
if ($new->created_at->diffInDays() > 30) {
    $timestamp = 'Created at ' . $new->created_at->toFormattedDateString();

// Else get the difference for humans
} else {
    $timestamp = 'Created ' $new->created_at->diffForHumans();
}

根据要求,我将给出一个完整集成的示例,说明我认为如何更好地实现。首先,我假设我可以在几个不同的地方,几个不同的视图上使用它,所以最好是将代码放在您的模型中,这样您就可以方便地从任何地方调用它,而无需任何麻烦

Post.php

class News extends Eloquent {

    public $timestamps = true;

    // ...

    public function formattedCreatedDate() {
        ìf ($this->created_at->diffInDays() > 30) {
            return 'Created at ' . $this->created_at->toFormattedDateString();
        } else {
            return 'Created ' . $this->created_at->diffForHumans();
        }
    }

}
然后,在视图文件中,只需执行
$news->formattedCreatedDate()
。例如:

<div class="post">
    <h1 class="title">{{ $news->title }}</h1>
    <span class="date">{{ $news->forammatedCreatedDate() }}</span>
    <p class="content">{{ $news->content }}</p>
</div>

{{$news->title}
{{$news->forammatedCreatedDate()}

{{{$news->content}

需要碳:

use Carbon\Carbon;
并使用它:

$user = User::find(2);

echo $user->created_at->diffForHumans( Carbon::now() );
你应该得到这个:

19 days before

很酷的故事。到目前为止你做了什么?你能把这个答案更具体一些,以表明你将如何在视图级别上整合它吗?这对我很有用。谢谢我相信你不必通过碳::now()