Voting

: eight minus one?
(Example: nine)

The Note You're Voting On

theking2 at king dot ma
29 days ago
Working on a (REST) interface between JavaScript and a database needs to take care of the problem of the time zone info. In JavaScript JSON.stringify() will convert all dates to UTC. Makes sense. If we receive and decode this on in the PHP realm we should explicitly say so and altought null indicates the configured locale timezone although I do believe that more often than not no timezone is configured in PHP. So better be save than sorry and prevent you to miss your flight:

<?php
$jsonString
= '{ "date": "2025-05-04T11:58:37.848Z" }';
$dateString = json_decode($jsonString, true)['date']; // will contain the date in UTC (Zulu) tz

$d = new \DateTimeImmutable($dateString, new \DateTimeZone('UTC')); // interpreted as such

$databaseZone = new \DateTimeZone("Europe/Zurich");
$d = $d->setTimeZone( $databaseZone ); // but our server is somewhere else

var_dump($d);

// Now we can store the date in our local database, which is blissfully unaware of timezones

?>

<< Back to user notes page

To Top