diff --git a/src/Jenssegers/Mongodb/Query/Builder.php b/src/Jenssegers/Mongodb/Query/Builder.php index 74b9ee3a6..1ec8565f3 100644 --- a/src/Jenssegers/Mongodb/Query/Builder.php +++ b/src/Jenssegers/Mongodb/Query/Builder.php @@ -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 @@ -894,7 +905,7 @@ protected function compileWheres() // Merge the compiled where with the others. $compiled = array_merge_recursive($compiled, $result); } - + return $compiled; } @@ -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); + } + return $value; + } /** * Handle dynamic method calls into the method. diff --git a/tests/QueryBuilderTest.php b/tests/QueryBuilderTest.php index ca08bf36a..bd843d223 100644 --- a/tests/QueryBuilderTest.php +++ b/tests/QueryBuilderTest.php @@ -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()