I always store dates and times as unix timestamps in an integer field. That way, I know exactly what I want to get from my database. I would recommend storing the time zone in a text field using string representations in this list , which you already know about.
For example, such a table is called stackoverflow, the database also calls stackoverflow: -
+--+----------+-------------+ |id|date_time |time_zone | +--+----------+-------------+ |1 |1373212914|Europe/London| |2 |1373212914|Europe/Rome | +--+----------+-------------+
Then you moisten the DateTime objects like this: -
$dsn = 'mysql:dbname=stackoverflow;host=127.0.0.1'; $user = 'stackoverflow'; $password = 'stackoverflow'; try { $dbh = new PDO($dsn, $user, $password); $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); } catch (PDOException $e) { echo 'Connection failed: ' . $e->getMessage(); } $sql = 'select date_time, time_zone from stackoverflow'; $statement = $dbh->prepare($sql); $statement->execute(); $results = $statement->fetchAll(); foreach($results as $result){ $datetimes[] = (new \DateTime())->setTimestamp((int)$result['date_time'])->setTimezone(new \DateTimeZone($result['time_zone'])); } var_dump($datetimes);
What gives this conclusion: -
array (size=2) 0 => object(DateTime)[3] public 'date' => string '2013-07-07 17:01:54' (length=19) public 'timezone_type' => int 3 public 'timezone' => string 'Europe/London' (length=13) 1 => object(DateTime)[4] public 'date' => string '2013-07-07 18:01:54' (length=19) public 'timezone_type' => int 3 public 'timezone' => string 'Europe/Rome' (length=11)
Checking the table shows that both records have the same time 2013-07-07 16:01:54 UTC , which corresponds to the value 2013-07-07 16:01:54 UTC , but displays correctly in the saved time zone when sending to the client.
source share