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
namespace App\Providers; use Illuminate\Database\Query\Builder as QueryBuilder; use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
Adding a whereActive macro in Laravel
macros
service-provider
query-builder
Intermediate
5 steps
java
public final class HttpHeaders { private final NavigableMap<String, List<String>> values = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
A case-insensitive HTTP headers map in Java
case-insensitivity
multimap
immutability
Intermediate
7 steps
java
public record ServerConfig( String host, int port, Duration timeout,
Loading typed config into a Java record
records
factory-methods
parsing
Intermediate
7 steps
php
<?php namespace App\Http\Middleware;
Verifying GitHub webhook signatures in Laravel
middleware
hmac
webhooks
Intermediate
5 steps
php
<?php namespace App\Models;
How Eloquent scopes model order status in Laravel
query scopes
state machine
eloquent
Intermediate
7 steps
ruby
class Order class InvalidTransition < StandardError; end TRANSITIONS = {
A state machine for order transitions in Ruby
state-machine
data-driven
error-handling
Intermediate
8 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.