whereBetween类型日期在laravel上而不考虑年

时间:2015-12-05 07:36:22

标签: php laravel eloquent

我想在不考虑年份的情况下显示日期在05-27和05-29(m-d)之间的数据。

示例查询可能如下所示:

employee::whereBetween('birthday', array('05-27', '05-29'))->get();

例如,如果我有5个项目: 1989年5月27日, 1989年5月29日, 1992年5月27日, 1993年5月27日, 1993年5月28日

应显示所有项目。这可能吗?

2 个答案:

答案 0 :(得分:1)

您可以像这样创建advanced queries来实现它。

Employee::select('*')
    ->whereMonth('birthday', '=', '5')
    ->where(function ($query) {
        $query->whereDay('birthday', '>=', 27)
              ->whereDay('birthday', '<=', 29);
    })
    ->get();

答案 1 :(得分:0)

使用日期时始终尝试使用Carbon

$first = Carbon::create(null,5,27);
$second = Carbon::create(null,5,29);
$birthdayBoys = employee::whereBetween('birthday',[$first,$second])->get();

修改

如果您的birthday列数据不是Carbon的实例,请在Employee模型中添加此行:

protected $dates = ['birthday']

它会自动将您的生日值存储在数据库中Carbon个实例。