php
47 lines · 7 steps
Scheduling reminders across time zones in PHP
A scheduler class converts between a user's local time and UTC so daily digests fire at the right moment.
Explained by
highlit
1<?php
2
3namespace App\Reminders;
4
5use DateTimeImmutable;
6use DateTimeZone;
7
8final class ReminderScheduler
9{
10 public function localToUtc(string $localTime, string $userTimezone): DateTimeImmutable
11 {
12 $local = new DateTimeImmutable($localTime, new DateTimeZone($userTimezone));
13
14 return $local->setTimezone(new DateTimeZone('UTC'));
15 }
16
17 public function utcToLocal(DateTimeImmutable $utc, string $userTimezone): DateTimeImmutable
18 {
19 return $utc->setTimezone(new DateTimeZone($userTimezone));
20 }
21
22 public function scheduleDailyDigest(string $userTimezone, string $sendAt = '08:00'): DateTimeImmutable
23 {
24 $tz = new DateTimeZone($userTimezone);
25 $now = new DateTimeImmutable('now', $tz);
26 $next = $now->modify("today {$sendAt}");
27
28 if ($next <= $now) {
29 $next = $next->modify('+1 day');
30 }
31
32 return $next->setTimezone(new DateTimeZone('UTC'));
33 }
34
35 public function displayForUser(DateTimeImmutable $utc, string $userTimezone): string
36 {
37 $local = $this->utcToLocal($utc, $userTimezone);
38 $offset = $local->format('P');
39
40 return sprintf(
41 '%s (%s, UTC%s)',
42 $local->format('D, M j \a\t g:i A'),
43 $userTimezone,
44 $offset
45 );
46 }
47}
01 / 01
STEP 01
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1Store timestamps in UTC and convert to a user's zone only at the boundaries of your system.
- 2DateTimeImmutable returns new instances, so every modify or setTimezone call must be reassigned to take effect.
- 3Anchoring 'today HH:MM' in the user's own timezone before converting keeps digests aligned to their local clock.
Related explainers
php
<?php class NameParser {
Parsing a full name into components in PHP
string-parsing
arrays
normalization
Intermediate
8 steps
php
<?php namespace App\Services\Checkout;
Validating coupons with Laravel's Pipeline
pipeline
chain of responsibility
transactions
Intermediate
7 steps
php
<?php namespace App\Services;
How a password strength validator works in PHP
validation
regular-expressions
data-driven
Intermediate
8 steps
php
<?php namespace App\Services;
Building a cached daily leaderboard in Laravel
caching
aggregation
eager-loading
Intermediate
9 steps
go
package logging import ( "context"
Deduplicating log attributes in Go's slog
decorator-pattern
structured-logging
immutability
Intermediate
8 steps
php
<?php final class RotatingFileLogger {
How a rotating file logger works in PHP
logging
file-rotation
io
Intermediate
9 steps
Share this explainer
Here's the card — post it anywhere.
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code
Embed this explainer
Drop the interactive walkthrough into a blog or docs. Views never cost a credit.
<iframe src="https://highlit.co/explainers/scheduling-reminders-across-time-zones-in-php-explained-php-1350/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.