Skip to content

Convert whereBetween values to UTCDateTime #953

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 27 additions & 2 deletions src/Jenssegers/Mongodb/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -866,7 +866,18 @@ protected function compileWheres()

// Convert DateTime values to UTCDateTime.
if (isset($where['value']) and $where['value'] instanceof DateTime) {
$where['value'] = new UTCDateTime($where['value']->getTimestamp() * 1000);
$where['value'] = $this->dateTimeConvertion($where['value']);
}

// Convert DateTime values to UTCDateTime in $where['values'] key.
if (array_key_exists('values', $where)) {
if (is_array($where['values']) && !empty($where['values'])) {
foreach ($where['values'] as $keyWhere => $valueWhere) {
if ($valueWhere instanceof DateTime) {
$where['values'][$keyWhere] = $this->dateTimeConvertion($valueWhere);
}
}
}
}

// The next item in a "chain" of wheres devices the boolean of the
Expand Down Expand Up @@ -894,7 +905,7 @@ protected function compileWheres()
// Merge the compiled where with the others.
$compiled = array_merge_recursive($compiled, $result);
}

return $compiled;
}

Expand Down Expand Up @@ -1018,6 +1029,20 @@ protected function compileWhereRaw($where)
{
return $where['sql'];
}

/**
* Convert to MongoDB\BSON\UTCDateTime if $value is a DateTime object.
*
* @param mixed $value
* @return mixed
*/
protected function dateTimeConvertion($value)
{
if ($value instanceof DateTime) {
return new UTCDateTime($value->getTimestamp() * 1000);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why UTCDateTime instead UTCDatetime?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According PHP documentation, the class name is UTCDateTime

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't the if conditional check redundant? You are already checking if the value is an instance of DateTime before calling the dateTimeConversion function.

}
return $value;
}

/**
* Handle dynamic method calls into the method.
Expand Down
6 changes: 6 additions & 0 deletions tests/QueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,12 @@ public function testDates()

$users = DB::collection('users')->whereBetween('birthday', [$start, $stop])->get();
$this->assertEquals(2, count($users));

$dateTimeStart = new DateTime("1981-01-01 00:00:00");
$dateTimeStop = new DateTime("1982-01-01 00:00:00");

$usersWithDateTimeFilter = DB::collection('users')->whereBetween('birthday', [$dateTimeStart, $dateTimeStop])->get();
$this->assertEquals(2, count($usersWithDateTimeFilter));
}

public function testOperators()
Expand Down