Dean
Dean

Reputation: 763

Retrieving the last 7 days with RethinkDB during()

I have been trying for some hours now to try and return all results in the last 7 days, using RethinkDB and PHP.

Surveying the documentation at https://www.rethinkdb.com/docs/dates-and-times/javascript/ I have found the most appropriate RethinkDB function would be during().

Below is my code that I have come to the realization that this task is far harder than it appears. I have attempted to use the DateTime from PHP directly into the during, but this was also not successful.

$sevenago = new DateTime('7 days ago');
$sevenago->setTimeZone(new DateTimeZone('Asia/Kolkata'));
$sevenago = date_format($sevenago, 'U');
$now = new DateTime();
$now->setTimeZone(new DateTimeZone('Asia/Kolkata'));
$now = date_format($now, 'U');

$ordersLastWeek = r\table("orders")->filter(function($doc) {
    return $doc('status')->eq('shipped')
    ->rAnd($doc('time')->during(r\epochTime($sevenago), r\epochTime($now)));
})->run($conn);

The time field is stored as follows (standard RethinkDB DateTime type):

{"$reql_type$":"TIME","epoch_time":1509557927.661,"timezone":"+00:00"}

Any help would be appreciated.

Upvotes: 1

Views: 112

Answers (2)

zstate
zstate

Reputation: 2055

Try this:

$ordersLastWeek = r\table("orders")->filter(function($doc) use ($sevenago, $now) {
    return $doc('status')->eq('shipped')->rAnd($doc('time')->during(r\epochTime($sevenago), r\epochTime($now)));
})->run($conn);

In your code you forgot to use use ($sevenago, $now) for the filter function.

Upvotes: 1

nammalvar 2.0
nammalvar 2.0

Reputation: 111

$ordersLastWeek = r\table("orders")->filter(function($doc) {
    return $doc('status')->eq('shipped')
    ->rAnd(($doc('time') > r\epoch_time($sevenago)));
})->run($conn);

Have you tried this. Hope it works.

Upvotes: 0

Related Questions