Skip to content
Open
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
4 changes: 2 additions & 2 deletions src/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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',
Expand Down
30 changes: 30 additions & 0 deletions tests/Data/Entries/EntryQueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down Expand Up @@ -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]]);
Expand Down
Loading