2015-10-07 23 views
0

我有一个名为RegistrationDate的字段,我需要检查时间。Laravel 4雄辩 - 时间戳超过3年

我需要一个地方克劳斯,会做:

if (time() - strtotime($car->RegistrationDate) > 3*365.25*24*60*60) 
    over 3 years old 
} 

所以:

$cars::where('RegistrationDate', '>', '3 years old')->get(); 

,但不知道该怎么做,我的日期将被保存为时间戳:2009-07- 17 00:00:00。

谢谢。

回答

1

使用碳库。我认为它已经被laravel框架加载了。但是,如果不是,你可以通过作曲者加载。 https://github.com/briannesbitt/Carbon

$cars::where('RegistrationDate', '>', Carbon\Carbon::now()->subYears(3))->get();假设$cars是你的Car模型的雄辩物体;

此外,与问题无关......但您的表命名约定有点奇怪。

$cars = Car::where('registration_date', '>', Carbon\Carbon::now()->subYears(3))->get();

+0

谢谢,不是我的代码/表悲伤地命名。 – Lovelock

0

您还可以使用DateTimeDateTimeInterval类:

$currentDate = new DateTime(); // get current date time 
$currentDate->sub(new DateInterval('P3Y')); // substract 3 years from current date 

之后,你可以在你的代码中使用$currentDate,如:

$cars::where('RegistrationDate', '>', $currentDate)->get(); 

你也可以转换$currentDate到SQL您需要的日期时间字符串:

$currentDateStr = $currentDate->format('Y-m-d H:i:s');