PHP

Convert Timestamp to User Timezone

admin by @admin ADMIN
1d ago
May 31, 2026
Public
0 0 up · 0 down Sign in to vote
Convert a stored UTC timestamp into the viewer's timezone for display. Round-trip safe: re-converting to UTC always gives back the original timestamp.
PHP
Raw
<?php
function toUserTz(int|string $utcTime, string $tz = 'America/Chicago', string $format = 'Y-m-d H:i T'): string {
    $t  = is_int($utcTime) ? $utcTime : strtotime($utcTime . ' UTC');
    $dt = new DateTime('@' . $t);          // always UTC
    $dt->setTimezone(new DateTimeZone($tz));
    return $dt->format($format);
}

echo toUserTz(1735689600, 'America/Chicago'); // 2024-12-31 18:00 CST
echo toUserTz(1735689600, 'Asia/Tokyo');      // 2025-01-01 09:00 JST
Tags

Save your own code snippets

Create a free account and build your private vault. Share publicly whenever you want.