A time bug doesn't crash the app. It shows the wrong time, and you hear about it from a customer. This note is the time standard I use in my Laravel APIs: one zone inside, one format in the contract, and convert only at the edge.
The problem
We wanted to publish offers and products that are only available in a time window: they start at one time and end at another. The app stores times in one zone. Users, API clients and admins are in different zones.
The first bug I hit wasn't even about users. In one project the app ran on UTC and the PostgreSQL session ran on Asia/Damascus. Laravel sends dates to the database without an offset, so the database read every UTC time as Damascus time. Every comparison was 3 hours off, and a reservation whose time had passed was never found.
Admins add a second problem. An admin in Damascus sets a weekend sale, 20% off, from 09:00 to 21:00 Damascus time. The app saves 09:00 as if it were UTC, so the sale starts at 12:00 in Damascus and 10:00 in Berlin. It is 3 hours late, and the code has no way to know.
Showing the zone in the UI
You can show the zone in the UI: "All times are UTC." Now every user and every admin has to convert by hand, every time they read or type a date. That's bad UX, and people get the math wrong.
The idea: one timestamp standard
Pick one standard from day one and make every part of the app follow it:
- One zone inside. The app, the database session and the columns all agree.
- One format in the API contract. Every timestamp has the same shape, so every client can parse it.
- Convert only at the edge. On the way in, from the sender's zone to the app zone. On the way out, to the reader's zone, or leave it to the client. The edge is the one place where you know whose time it is.
It comes in two levels. Level 1 is what I ship today. Level 2 is the next step when your app needs it.

Before and after the timestamp standard, with one weekend sale.
Level 1: UTC inside, UTC out, the client converts
This is enough for most apps. The server never needs to know where the user is.
- One zone for the app and the database session. Don't trust the server's default.
// config/app.php
'timezone' => 'UTC',
// config/database.php, the pgsql connection
'timezone' => 'UTC',
- Columns that keep the zone.
$table->timestampTz('starts_at');
$table->timestampTz('ends_at');
- One format, written once in the API contract. UTC, ISO 8601, milliseconds,
Z.
Timestamp:
type: string
format: date-time
pattern: '^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}Z$'
example: '2026-11-14T09:00:00.000Z'
- One formatter for every response. Resources, model serialization and CSV exports all call it, so the format can't drift.
final class Time
{
public const FORMAT = 'Y-m-d\TH:i:s.v\Z';
public static function out(?DateTimeInterface $at): ?string
{
return $at ? CarbonImmutable::instance($at)->utc()->format(self::FORMAT) : null;
}
}
// in a resource
'starts_at' => Time::out($this->starts_at),
- Strict input. A date without an offset is a guess, so refuse it. Convert to UTC before saving, because Laravel writes dates without their offset.
$data = $request->validate([
'starts_at' => ['required', 'date', 'regex:/(Z|[+-]\d{2}:\d{2})$/'],
]);
$offer->starts_at = CarbonImmutable::parse($data['starts_at'])->utc();
- Tests that pin it. If someone changes the database zone or formats a date by hand, the build breaks first.
it('runs the database session in UTC', function () {
expect(DB::selectOne('show timezone')->TimeZone)->toBe('UTC');
});
it('sends times in the contract format', function () {
$offer = Offer::factory()->create(['starts_at' => '2026-11-14 09:00:00']);
$this->getJson("/api/offers/{$offer->id}")
->assertJsonPath('data.starts_at', '2026-11-14T09:00:00.000Z');
});
The client gets 2026-11-14T09:00:00.000Z and shows 12:00 in Damascus or 10:00 in Berlin. The server did no math.
Where Level 1 stops
Level 1 is clean until the server itself needs a local time:
- "Today" is a UTC day. An offer that is valid "today" switches at 03:00 for a user in Damascus. Reports grouped by day use UTC days too.
- Admins type local times. A date picker with no zone saves the weekend sale's 09:00 as 09:00 UTC, so the sale starts 3 hours late, like in the diagram above.
Level 2: the server knows whose time it is
This is the design I'm moving to. The rule stays the same: one zone inside, convert at the edge. The edge just learns the zone of the person on the other side.
- Admins: a timezone on each admin account. Filament loads every date in that zone and converts back to the app zone on save.
class UseAdminTimezone
{
public function handle(Request $request, Closure $next)
{
FilamentTimezone::set($request->user()?->timezone ?? config('app.timezone'));
return $next($request);
}
}
// in the panel provider
->authMiddleware([Authenticate::class, UseAdminTimezone::class], isPersistent: true)
The account form validates the field with Laravel's timezone rule, so only real zone names get saved.
- API clients: the client sends its zone once per request, and one middleware reads it for every endpoint.
class ClientTimezone
{
public function handle(Request $request, Closure $next)
{
$zone = $request->header('X-Timezone', 'UTC');
if (! in_array($zone, DateTimeZone::listIdentifiers(), true)) {
throw new AppException(CoreErrorCode::BadRequest, ['header' => 'X-Timezone']);
}
app()->instance('client.timezone', $zone);
return $next($request);
}
}
(AppException and the error codes are from Built Notes 01.)
- "Today" in the client's zone, compared in UTC.
$zone = app('client.timezone');
$offers = Offer::query()
->where('starts_at', '<=', now($zone)->endOfDay()->utc())
->where('ends_at', '>=', now($zone)->startOfDay()->utc())
->get();
- Answer in the client's zone. It's the same moment, written with the client's offset, and any ISO 8601 parser reads it.
public static function forClient(?DateTimeInterface $at): ?string
{
return $at
? CarbonImmutable::instance($at)->setTimezone(app('client.timezone'))->format('Y-m-d\TH:i:s.vP')
: null;
}
The same offer, set by an admin in Berlin for 10:00 local time:
- stored:
2026-11-14 09:00:00+00 - a client sending
X-Timezone: Asia/Damascusgets2026-11-14T12:00:00.000+03:00 - a client with no header gets
2026-11-14T09:00:00.000+00:00
Why it pays
- No more "3 hours off" bugs between the app and the database.
- Every client parses one format, and a test stops anyone from changing it.
- Users and admins see their own time and never do the math.
Other frameworks
Same rule anywhere: one zone in the database session, one format in the contract, convert in the serializer (Jackson, DRF, JSON.stringify with toISOString()).
Not covered here
This standard fits my case. Real systems often have other requirements, and I skipped them on purpose:
- Internal and external services (payment providers, partners, your own other services) send times in their own format and zone. Each one is another edge. Convert there, and keep the raw value they sent.
- Some apps must remember the zone a time was set in, not only the moment. A meeting at 09:00 in Berlin should still be 09:00 after a daylight saving change, so you store the zone name next to the time.
- When microservices compare times, their clocks have to agree. Every server syncs with an NTP server, or one service's "now" runs ahead of another's.
If one of these is your case, the rule still helps, but you need more than this note. This note can help you to start, and the main idea is the same: make a standard for times that all components of the system follow.
Takeaway
Define how you store timestamps from day one. It saves you time fixing and debugging later, and keeps your app's data consistent and integrated with every part, internal and external.
Originally published on aristonis.org.
Top comments (0)