john doe23 john doe23 - 7 months ago 91
PHP Question

date_default_timezone_set(“Europe/London”) does not return correct time

When I use the code below to set my default timezone and insert it into a database, the time is in-correct.

At the time of testing, the time was 11:56, but the database has it set at 06:56, 5 hours behind.

<?php
date_default_timezone_set('Europe/London');
$stmt = $con->prepare("INSERT INTO test (datetime) VALUES (CURRENT_TIMESTAMP)");
$stmt->execute();
?>

Answer

You can get the time in php

<?php
date_default_timezone_set('Europe/London');
$time_stamp = date('Y-m-d H:i:s');
$stmt = $con->prepare("INSERT INTO test (datetime) VALUES ('".$time_stamp."')");
$stmt->execute();
?>