Reputation: 1398
I am making an application where I need to store th date in MySQL using the PHP date()
function.
<?php $finalize_at = date('Y-m-d H:i:s'); ?>
These dates need to be compared in MySQL using the NOW()
function to return the difference in hours, for example:
SELECT TIMESTAMPDIFF( hour, NOW(), finalize_at ) FROM plans;
But the problem is – the PHP date function date('Y-m-d H:i:s')
uses the PHP timezone setting, and the NOW()
function takes the MySQL timezome from the MySQL server.
I'm trying to solve doing this:
date_default_timezone_set('Europe/Paris');
It works only for PHP.date.timezone= "Europe/Paris";
It works only for PHP.SELECT CONVERT_TZ(now(), 'GMT', 'MET');
This return empty.mysql> SET time_zone = 'Europe/Paris';
This throws an error from the console of MySQL.And the timezone does not change for MySQL.
Is there any way to change the timezone for both PHP and MySQL without having to do it from the MySQL console, or set a timezone change from somewhere in php.ini
and make these values available for both PHP and MySQL.
Much appreciate your support.
Upvotes: 30
Views: 58278
Reputation: 6737
Thanks to the other answers I found the best solution for me, with a few lines of code and without the need to modify any server configuration file.
1) set the desired timezone in PHP:
date_default_timezone_set('Europe/Rome');
2) establish the PDO connection to MySql:
$MySqlConnection = new PDO("mysql:host=myHost;dbname=MyDb;charset=myCharset", $myUser, $myPwd);
3) set the same timezone in the PDO resource:
$MySqlConnection->exec("SET TIME_ZONE = '".date('P')."';");
Since MySQL may not recognize the timezone name Europe/Rome
, use date('P')
to pass it in the +/-HH:mm
format.
It is important to note that this approach allows you to manage the transition from summer time to winter time and vice versa without having to change the code.
Upvotes: 2
Reputation: 21
For PHP use this function:
date_default_timezone_set()
MySQL:
default-time-zone='timezone'
If you have the root privilege, you can set the global server time zone value at run-time with this statement:
SET GLOBAL time_zone = timezone;
Per-connection time zones. Each client that connects has their own time zone setting, given by the session time_zone variable
. Initially, the session variable takes its value from the global time_zone
variable, but the client can change its own time zone with this statement:
SET time_zone = timezone;
Upvotes: 2
Reputation: 636
The best method Set timezone in PDO MySQL:
If appropriate, whether by mistake you can use: date('P') Example:
new PDO('mysql:host=localhost;dbname=nametable',
'username',
'password',
[PDO::MYSQL_ATTR_INIT_COMMAND =>"SET NAMES utf8;SET time_zone = '".date('P')."'"]);
Upvotes: 4
Reputation: 636
new PDO('mysql:host=localhost;dbname=nametable',
'username',
'password',
[PDO::MYSQL_ATTR_INIT_COMMAND =>"SET NAMES utf8;SET time_zone = 'Europe/Rome'"]);
Upvotes: 1
Reputation: 757
You can do it easily just by the following two lines of PHP.
$tz = (new DateTime('now', new DateTimeZone('Asia/Kabul')))->format('P');
$pdo->exec("SET time_zone='$tz';");
Upvotes: 17
Reputation: 13323
In PHP:
<?php
define('TIMEZONE', 'Europe/Paris');
date_default_timezone_set(TIMEZONE);
For MySQL:
<?php
$now = new DateTime();
$mins = $now->getOffset() / 60;
$sgn = ($mins < 0 ? -1 : 1);
$mins = abs($mins);
$hrs = floor($mins / 60);
$mins -= $hrs * 60;
$offset = sprintf('%+d:%02d', $hrs*$sgn, $mins);
//Your DB Connection - sample
$db = new PDO('mysql:host=localhost;dbname=test', 'dbuser', 'dbpassword');
$db->exec("SET time_zone='$offset';");
The PHP and MySQL timezones are now synchronized within your application. No need to go for php.ini
or MySQL console!
This is from this article on SitePoint.
Upvotes: 41
Reputation:
I can change my mysql default timezone from variable section by editing row which says "time zone" in phpmyadmin.
Here you can also change format and lot more you can find in mysql support http://dev.mysql.com/doc/refman/5.7/en/time-zone-support.html, I hope its help you.
You can put same timezone for both php and mysql.
Upvotes: 6