2014-10-10 88 views
1

我有这个领域$data->created_at并希望只显示数据,如果它是> 30 minutes比较日期在laravel

在我看来,我有:

@if ($data->created_at > strtotime('30 minutes')) 
    show data 
@endif 

这是行不通的。我也在控制器中构建了这个语句,但它不起作用。它在某种程度上似乎受到了碳的影响。

Laravel的最佳做法是什么?

回答

2

Laravel的created_at和updated_at字段是\ DateTime对象。你应该能够做到这一点使用

@if($data->created_at > \new DateTime('-30 minutes')) 
    show data 
@endif 
+0

从技术上讲,他们是碳对象。 – Dwight 2014-10-11 23:51:14

+0

技术上碳对象是DateTime对象。 – 2014-10-13 06:59:07

+0

也是如此,但值得知道它们是Carbon,因为它在DateTime之上提供了很多附加功能。 – Dwight 2014-10-13 10:18:41

3

这样做将是一个更加“Laravel方式”,

@if($data->created_at > (new \Carbon\Carbon())->subMins(30)) 
    show data 
@endif