diff --git a/app/Http/Controllers/Posts/PostController.php b/app/Http/Controllers/Posts/PostController.php
index 9d72798d..6ec3c7d0 100644
--- a/app/Http/Controllers/Posts/PostController.php
+++ b/app/Http/Controllers/Posts/PostController.php
@@ -14,12 +14,14 @@
use App\Services\LdJsonService;
use App\Services\PostService;
use App\Traits\PostTrait;
+use App\Traits\SessionTimezoneTrait;
use App\Traits\SubsiteTrait;
use Illuminate\Contracts\View\View;
final class PostController extends BaseController
{
use PostTrait;
+ use SessionTimezoneTrait;
use SubsiteTrait;
protected int $subsiteId;
@@ -55,6 +57,7 @@ public function show(Post $post): View
'next' => $post->next(),
'previous' => $post->previous(),
'canonicalUrl' => $this->getCanonicalUrl($post),
+ 'displayTimezone' => $this->getDisplayTimezone(),
'relatedPosts' => $relatedPosts,
'subdomain' => $subdomain,
'useLivewire' => true,
diff --git a/app/Livewire/Comments/CommentComponent.php b/app/Livewire/Comments/CommentComponent.php
index cf65ed09..0022fef1 100644
--- a/app/Livewire/Comments/CommentComponent.php
+++ b/app/Livewire/Comments/CommentComponent.php
@@ -10,6 +10,7 @@
use App\Models\Post;
use App\Models\User;
use App\Traits\CommentComponentTrait;
+use App\Traits\SessionTimezoneTrait;
use Illuminate\Contracts\View\View;
use Illuminate\Support\Facades\DB;
use Livewire\Attributes\On;
@@ -18,6 +19,7 @@
final class CommentComponent extends Component
{
use CommentComponentTrait;
+ use SessionTimezoneTrait;
// Data
public ?int $authorizedUserId;
diff --git a/app/Livewire/Localization/SwitchTimezoneComponent.php b/app/Livewire/Localization/SwitchTimezoneComponent.php
new file mode 100644
index 00000000..8cb529b4
--- /dev/null
+++ b/app/Livewire/Localization/SwitchTimezoneComponent.php
@@ -0,0 +1,19 @@
+ $posts,
+ 'displayTimezone' => $this->getDisplayTimezone(),
]);
}
@@ -72,7 +75,7 @@ public function query(): Builder
private function getPosts(): CursorPaginator
{
$dateQueries = [
- DB::raw('DATE_FORMAT(posts.created_at, "%m-%d") as month_day'),
+ DB::raw("DATE_FORMAT(CONVERT_TZ(posts.created_at, 'UTC', " . DB::connection()->getPdo()->quote($this->getDisplayTimezone()) . "), '%m-%d') as month_day"),
DB::raw('COUNT(*) as total_posts'),
];
diff --git a/app/Traits/SessionTimezoneTrait.php b/app/Traits/SessionTimezoneTrait.php
new file mode 100644
index 00000000..c4e53843
--- /dev/null
+++ b/app/Traits/SessionTimezoneTrait.php
@@ -0,0 +1,39 @@
+displayTimezone is null.
+ return $this->displayTimezone ?? session('displayTimezone') ?? config('app.timezone');
+ }
+
+ public function setDisplayTimezone(string $timezone): void
+ {
+ try {
+ // Try to create a CarbonTimezone object to validate the timezone string.
+ CarbonTimezone::create($timezone);
+
+ // If we get here, the timezone is valid, so we can set it.
+ $this->displayTimezone = $timezone;
+ } catch (InvalidTimeZoneException $e) {
+ $this->logError('Invalid timezone: ' . $timezone);
+ }
+ }
+}
diff --git a/app/View/Components/Dates/FormattedDateTimeComponent.php b/app/View/Components/Dates/FormattedDateTimeComponent.php
index e165f673..d9d27788 100644
--- a/app/View/Components/Dates/FormattedDateTimeComponent.php
+++ b/app/View/Components/Dates/FormattedDateTimeComponent.php
@@ -4,12 +4,15 @@
namespace App\View\Components\Dates;
+use App\Traits\SessionTimezoneTrait;
use Carbon\Carbon;
use Illuminate\Contracts\View\View;
use Illuminate\View\Component;
final class FormattedDateTimeComponent extends Component
{
+ use SessionTimezoneTrait;
+
public Carbon $date;
public string $format = 'Y-m-d H:i:s';
@@ -36,7 +39,8 @@ public function render(): View
private function getFormattedDate(): string
{
- $formattedDate = $this->date->format($this->format());
+ $date = $this->date->tz($this->getDisplayTimezone());
+ $formattedDate = $date->format($this->format());
return $this->addPeriods($formattedDate);
}
diff --git a/resources/views/comments/partials/comment-timestamp.blade.php b/resources/views/comments/partials/comment-timestamp.blade.php
index a6f24c2c..7883a5c9 100644
--- a/resources/views/comments/partials/comment-timestamp.blade.php
+++ b/resources/views/comments/partials/comment-timestamp.blade.php
@@ -1,11 +1,11 @@
diff --git a/resources/views/layouts/app.blade.php b/resources/views/layouts/app.blade.php
index 40cc2ef0..4d7404b4 100644
--- a/resources/views/layouts/app.blade.php
+++ b/resources/views/layouts/app.blade.php
@@ -21,6 +21,8 @@
@endif
+