Siegmeyer Siegmeyer - 4 months ago 39
PHP Question

Laravel: How to filter objects by date, where date is a UNIX timestamp?

I'm starting with Laravel (

4.2
) and I would like to filter table's rows by column which is a UNIX timestamp. Specifically I need
timestamp
to be in some (date) range.

I imagine it would look something like:

$objs = DB::table('[table name]')->where('timestamp', [condition])->get();


I'm using
mongodb 2.2.3
.

Answer

What you're looking for is whereBetween.

$range = [timestamp1, timestamp2];
->whereBetween('timestamp', $range);

Also note that all methods on the query builder support Closure:

->whereBetween('timestamp', function($q){
    //some logic
    return [timestamp1, timestamp2];
});