Laravelで日付の検索、YYYYMMDDの形式の場合。前方後方真ん中、年月日組み合わせ検索。
をつくってみた。
public static function set_date_search($query,$date,$targetKey){
$y = ($date['year'])?'1':'0';
$m = ($date['month'])?'1':'0';
$d = ($date['day'])?'1':'0';
$ymd = ''.$y.$m.$d;
switch($ymd){
case '001':
return $query->where($targetKey,'like','______'.$date['day']);
case '011':
return $query->where($targetKey,'like','____'.$date['month'].$date['day']);
case '100':
return $query->where($targetKey,'like',$date['year'].'____');
case '110':
return $query->where($targetKey,'like',$date['year'].$date['month'].'__');
case '010':
return $query->where($targetKey,'like','____'.$date['month'].'__');
case '101':
return $query->where($targetKey,'like',$date['year'].'__'.$date['day']);
case '111':
return $query->where($targetKey,'=',dateArr2DbString($date));
default:
return $query;
}
}
コメントを残す