Laravel:搜索日期,忽略时间部分

时间:2014-03-30 21:17:46

标签: php laravel-4

如何搜索created_at日期,忽略一天中的时间?

我现在这样做,但感觉有点像黑客:

$searchDate = strtotime($search);
if($searchDate !== false) {
    $q->orWhere(DB::raw('DATE(`created_at`)'), Carbon::createFromTimestamp($searchDate)->toDateString());
}

1 个答案:

答案 0 :(得分:2)

有一种方法可以做到:

$search_date = Carbon::createFromTimestamp($searchDate)->toDateString();

$q->where('created_at','LIKE', $search_date . '%')->get();

或者你可以这样写,

$search_date = Carbon::createFromTimestamp($searchDate)->toDateString();

$q->orWhereRaw('DATE(created_at) = ?'), array($search_date));