Carbon을 사용해보세요. 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();
}
요청으로
, 나는 그것을 할 수있는 더 좋은 방법이 될 것이라고 생각 방법에 대한 통합의 예를 줄 것이다. 먼저 여러 다른 장소, 여러보기에서이 코드를 사용할 수 있다고 가정합니다. 따라서 코드가 모델 내부에있는 것이 가장 좋으므로 번거 로움없이 어디에서나 편리하게 호출 할 수 있습니다.
는
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()
을 Post.php을 할 것입니다. 예 :
<div class="post">
<h1 class="title">{{ $news->title }}</h1>
<span class="date">{{ $news->forammatedCreatedDate() }}</span>
<p class="content">{{ $news->content }}</p>
</div>
쿨 이야기를. 지금까지 뭐 했니? – Jessica