diff --git a/src/Query/Builder.php b/src/Query/Builder.php index e6c1511e83..6d84d98abd 100644 --- a/src/Query/Builder.php +++ b/src/Query/Builder.php @@ -432,7 +432,7 @@ public function whereDate($column, $operator, $value = null, $boolean = 'and') $value = Carbon::parse($value); } - $value = Carbon::parse($value->format('Y-m-d')); // we only care about the date part + $value = Carbon::instance($value)->setTimezone(config('app.timezone'))->startOfDay(); // we only care about the date part $this->wheres[] = [ 'type' => 'Date', @@ -558,7 +558,7 @@ public function whereTime($column, $operator, $value = null, $boolean = 'and') $value = Carbon::parse($value); } - $value = $value->format('H:i:s'); // we only care about the time part + $value = Carbon::instance($value)->setTimezone(config('app.timezone'))->format('H:i:s'); // we only care about the time part $this->wheres[] = [ 'type' => 'Time', diff --git a/tests/Data/Entries/EntryQueryBuilderTest.php b/tests/Data/Entries/EntryQueryBuilderTest.php index 67f7168413..014f68545c 100644 --- a/tests/Data/Entries/EntryQueryBuilderTest.php +++ b/tests/Data/Entries/EntryQueryBuilderTest.php @@ -173,6 +173,21 @@ public function entries_are_found_using_where_date() $this->assertEquals(['Post 1', 'Post 3'], $entries->map->title->all()); } + #[Test] + public function entries_are_found_using_where_date_with_a_carbon_instance_in_a_different_timezone() + { + $this->createWhereDateTestEntries(); + + // 2021-11-15 02:00 in Moscow (+03:00) is 2021-11-14 23:00 in the app's (UTC) timezone, + // so it should match Post 2 (stored as 2021-11-14) and not Post 1 or Post 3 (2021-11-15). + $value = Carbon::create(2021, 11, 15, 2, 0, 0, 'Europe/Moscow'); + + $entries = Entry::query()->whereDate('test_date', $value)->get(); + + $this->assertCount(1, $entries); + $this->assertEquals(['Post 2'], $entries->map->title->all()); + } + #[Test] public function entries_are_found_using_where_month() { @@ -251,6 +266,21 @@ public function entries_are_found_using_where_time() $this->assertEquals(['Post 2'], $entries->map->title->all()); } + #[Test] + public function entries_are_found_using_where_time_with_a_carbon_instance_in_a_different_timezone() + { + $this->createWhereDateTestEntries(); + + // 2021-11-13 12:00 in Moscow (+03:00) is 09:00 in the app's (UTC) timezone, + // matching Post 2's stored time. + $value = Carbon::create(2021, 11, 13, 12, 0, 0, 'Europe/Moscow'); + + $entries = Entry::query()->whereTime('test_date', $value)->get(); + + $this->assertCount(1, $entries); + $this->assertEquals(['Post 2'], $entries->map->title->all()); + } + private function createWhereDateTestEntries() { $blueprint = Blueprint::makeFromFields(['test_date' => ['type' => 'date', 'time_enabled' => true]]);