29

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:

  1. date_default_timezone_set('Europe/Paris'); It works only for PHP.
  2. date.timezone= "Europe/Paris"; It works only for PHP.
  3. SELECT CONVERT_TZ(now(), 'GMT', 'MET'); This return empty.
  4. 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.

Paulie-C
  • 1,674
  • 1
  • 13
  • 29
Learning and sharing
  • 1,378
  • 3
  • 25
  • 45

7 Answers7

40

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.

Darryl Hein
  • 142,451
  • 95
  • 218
  • 261
Thamilhan
  • 13,040
  • 5
  • 37
  • 59
  • Thank you very much for the answer, and had seen this article, but I think it's a lot of work to do this, is there any way to make it easier? You can change the time zone myslq from php.ini? – Learning and sharing Dec 23 '15 at 05:06
  • 1
    I don't think adding a couple of lines makes your work harder! By the way you are not required to go to console for changes right? php.ini is for php configuration, I don't think there is a way out there for mysql configuration in that php's file – Thamilhan Dec 23 '15 at 05:08
  • I'm using CodeIgniter framework for query as the database, and I'm using these examples but displays an error, you will know how to apply this example in codeigniter? http://www.codeigniter.com/user_guide/database/query_builder.html#inserting-data – Learning and sharing Dec 23 '15 at 05:15
  • I haven't tried in CI, but I found SO answer here http://stackoverflow.com/a/20488347/5447994 Hope it might work for you. And an article: http://mytricks.org/set-deafult-timezone-codeigniter/ – Thamilhan Dec 23 '15 at 05:19
  • 1
    Excellent thank you very much, you have a good day, I would see an easier option as PHP would be perfect. – Learning and sharing Dec 23 '15 at 05:24
  • In the article you shared, shows a very practical example, specify in the query, the time zone example: mysql_query("SET time_zone = '+1:00'"); I'm doing it this way and it works, but I need to do it this way mysql_query("SET time_zone='Europe/Paris'"); and not working, will you have to adjust a parameter? – Learning and sharing Dec 23 '15 at 06:10
  • You should use `SET time_zone='$offset';` not `SET time_zone='Europe/Paris'` – Thamilhan Dec 23 '15 at 06:20
  • Can be rewritten as `$sec = (new DateTime())->getOffset(); $offset = ($sec < 0 ? '-' : '+') . gmdate('G:i', abs($sec)); db_query("SET time_zone='$offset'");` – Semra Aug 31 '16 at 13:16
  • 5
    You can use `$offset = date('P');` to skip a few steps – mike.k Sep 20 '16 at 20:46
16

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';");
MNR
  • 727
  • 1
  • 9
  • 23
6

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.

enter image description here

You can put same timezone for both php and mysql.

4

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')."'"]);
David Makogon
  • 69,407
  • 21
  • 141
  • 189
Clary
  • 544
  • 1
  • 7
  • 8
2

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;
John
  • 1
  • 13
  • 98
  • 177
Aalphin
  • 21
  • 2
1

The best method Set timezone in PDO MySQL:

new PDO('mysql:host=localhost;dbname=nametable', 'username', 'password', [PDO::MYSQL_ATTR_INIT_COMMAND =>"SET NAMES utf8;SET time_zone = 'Europe/Rome'"]);

Clary
  • 544
  • 1
  • 7
  • 8
1

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.

user2342558
  • 5,567
  • 5
  • 33
  • 54
  • Caution must be observered with long-standing SQL connections around the transition to and from summer time. – gileri Aug 23 '22 at 16:28