Reputation: 11
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();
?>
Upvotes: 0
Views: 1109
Reputation: 1374
Provided MySQL has information about timezone settings (it usually has):
SET time_zone = 'Europe/London'
If there is no information about timezones, but there is in the server, you can load the tables in MySQL using the command line on the server, but that's server management:
mysql_tzinfo_to_sql /usr/share/zoneinfo | mysql -u root mysql -p
If installing timezone tables is not possible, you can get the default timezone offset in the form +hh:mm
with php and use it as time_zone
in MySQL. This must be done, of course, after calling date_default_timezone_set('<timezone_name>')
$datetime = new DateTime();
$datetime_offset = $datetime->format("P");
$query = "SET time_zone = '$datetime_offset'";
Upvotes: 1
Reputation: 289
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();
?>
Upvotes: 1