从日期栏到年龄范围内的学者表?

时间:2016-12-16 15:40:46

标签: php laravel-5

我正在尝试创建一个标准来显示年龄范围内的年龄。使用此代码,其工作正常,但计算仅基于年份。如果计算包括当天也可能很好更准确。

var value = $('input[name="options"]:checked').data('name');
alert(value);

在第二个代码中: 它会产生错误......

database Scholar
scholar_id      scholar_birthday         
1               1991-12-19            
2               1990-01-19            
3               2000-03-19            
4               1992-21-19            
5               1991-12-19            
6               2000-10-12            
7               2001-12-23            
8               2009-12-19            

public function ListOrgaScholar($ship_id)
{
    $ship = Scholarship::find($ship_id);
    $ship_age_from = $ship->ship_age_from;
    $ship_age_to = $ship->ship_age_to;

    $scholars = (new Scholar)->newQuery()->select('*');

    $scholars->whereBetween(DB::raw('TIMESTAMPDIFF(YEAR,scholars.scholar_birthday,CURDATE())'),array($ship_age_from,$ship_age_to));

    $scholars = $scholars->get();


    dd($scholars);
}
result of dd($scholars)
Collection {#268 ▼
#items: array:3 [▼
0 => Scholar {#270 ▶}
1 => Scholar {#275 ▶}
2 => Scholar {#303 ▶}
]
}

错误

  

SQLSTATE [42000]:语法错误或访问冲突:1064您有   SQL语法错误;查看与您的手册相对应的手册   MariaDB服务器版本用于正确的语法使用附近   'scholars.scholar_placebirth,CURDATE())之间?和?'在第1行   (SQL:select * from public function ListOrgaScholar($ship_id) { $ship = Scholarship::find($ship_id); $ship_age_from = $ship->ship_age_from; $ship_age_to = $ship->ship_age_to; $scholars = (new Scholar)->newQuery()->select('*'); $scholars->whereBetween(DB::raw('TIMESTAMPDIFF(YEAR,MONTH,DAY,scholars.scholar_birthday,CURDATE())'),array($ship_age_from,$ship_age_to)); $scholars = $scholars->get(); dd($scholars); } where   TIMESTAMPDIFF(年,月,日,scholars.scholar_placebirth,CURDATE())   之间和)

我希望你能帮助我。

1 个答案:

答案 0 :(得分:0)

public function ListOrgaScholar($ship_id)
{
    $ship = Scholarship::find($ship_id);
    $ship_age_from = Carbon::now()->subYears($ship->ship_age_from);
    $ship_age_to = Carbon::now()->subYears($ship->ship_age_to);

    $scholars = Scholar::where('scholar_birthday', '>=', $ship_age_from->format('Y-m-d'))->where('scholar_birthday', '<=', $ship_age_to->format('Y-m-d')))->get();
}