diff --git a/src/Http/Middleware/Localize.php b/src/Http/Middleware/Localize.php index c882bd061da..aaa8908cc65 100644 --- a/src/Http/Middleware/Localize.php +++ b/src/Http/Middleware/Localize.php @@ -40,7 +40,7 @@ public function handle($request, Closure $next) return $date->forHumans(); } - return $date->setTimezone(Statamic::displayTimezone())->format(Statamic::dateFormat()); + return $date->copy()->setTimezone(Statamic::displayTimezone())->format(Statamic::dateFormat()); }); $response = $next($request); diff --git a/src/Modifiers/CoreModifiers.php b/src/Modifiers/CoreModifiers.php index 47e8dfcb1c7..34267fde42a 100644 --- a/src/Modifiers/CoreModifiers.php +++ b/src/Modifiers/CoreModifiers.php @@ -3352,7 +3352,7 @@ private function carbon($value) } if (config('statamic.system.localize_dates_in_modifiers')) { - $value->setTimezone(Statamic::displayTimezone()); + $value = $value->copy()->setTimezone(Statamic::displayTimezone()); } return $value; diff --git a/tests/FrontendTest.php b/tests/FrontendTest.php index 6c648253bca..39d3acb9fef 100644 --- a/tests/FrontendTest.php +++ b/tests/FrontendTest.php @@ -736,6 +736,26 @@ public function it_sets_the_carbon_to_string_format() $this->assertDefaultCarbonFormat(); } + #[Test] + public function outputting_a_date_does_not_localize_it_for_the_rest_of_the_template() + { + config([ + 'statamic.system.date_format' => 'H:i', + 'statamic.system.display_timezone' => 'Europe/Zurich', // +1 hour + 'statamic.system.localize_dates_in_modifiers' => false, + ]); + + $this->viewShouldReturnRaw('layout', '{{ template_content }}'); + $this->viewShouldReturnRaw('some_template', '
{{ date }}
{{ date format="H:i" }}
'); + + tap($this->makeCollection()->dated(true))->save(); + tap($this->makePage('about', ['with' => ['template' => 'some_template']])->date(Carbon::parse('2025-01-01 18:25')))->save(); + + $this->get('/about') + ->assertSee('19:25
', false) + ->assertSee('18:25
', false); + } + #[Test] public function it_sets_the_locale() { diff --git a/tests/Modifiers/FormatTest.php b/tests/Modifiers/FormatTest.php index 78bc8d7c7c8..4910785d210 100644 --- a/tests/Modifiers/FormatTest.php +++ b/tests/Modifiers/FormatTest.php @@ -30,6 +30,18 @@ public function it_formats_date_and_outputs_in_display_timezone() $this->assertSame('1st January 2025 4:45pm', $this->modify(Carbon::parse('2025-01-01 15:45'), 'jS F Y g:ia')); } + #[Test] + public function it_does_not_mutate_the_original_date_when_localizing() + { + config()->set('statamic.system.localize_dates_in_modifiers', true); + + $date = Carbon::parse('2025-01-01 15:45'); + + $this->modify($date, 'g:ia'); + + $this->assertSame('UTC', $date->timezone->getName()); + } + public function modify($value, $format) { return Modify::value($value)->format($format)->fetch();