在 PHP 和 MySQL 中设置时区

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34428563/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 23:58:51  来源:igfitidea点击:

Set timezone in PHP and MySQL

phpmysqldatedatetimetimezone

提问by Learning and sharing

I am making an application where I need to store th date in MySQL using the PHP date()function.

我正在制作一个应用程序,我需要使用 PHPdate()函数在 MySQL 中存储日期。

<?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:

这些日期需要在 MySQL 中使用NOW()函数进行比较以返回小时的差异,例如:

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.

但问题是——PHP 日期函数date('Y-m-d H:i:s')使用 PHP 时区设置,该NOW()函数从 MySQL 服务器获取 MySQL时区。

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.
  1. date_default_timezone_set('Europe/Paris');它仅适用于 PHP。
  2. date.timezone= "Europe/Paris";它仅适用于 PHP。
  3. SELECT CONVERT_TZ(now(), 'GMT', 'MET');此返回空。
  4. mysql> SET time_zone = 'Europe/Paris';这会从 MySQL 的控制台抛出错误。

And the timezone does not change for MySQL.

并且 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.iniand make these values available for both PHP and MySQL.

有什么方法可以更改 PHP 和 MySQL 的时区,而不必从 MySQL 控制台执行此操作,或者从某处设置时区更改php.ini并使这些值可用于 PHP 和 MySQL。

Much appreciate your support.

非常感谢您的支持。

回答by Thamilan

In PHP:

在 PHP 中:

<?php
define('TIMEZONE', 'Europe/Paris');
date_default_timezone_set(TIMEZONE);

For MySQL:

对于 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.inior MySQL console!

PHP 和 MySQL 时区现在在您的应用程序中同步。无需使用php.ini或 MySQL 控制台!

This is from this article on SitePoint.

这是来自SitePoint 上的这篇文章

回答by MNR

You can do it easily just by the following two lines of PHP.

只需通过以下两行 PHP 代码就可以轻松完成。

$tz = (new DateTime('now', new DateTimeZone('Asia/Kabul')))->format('P');
$pdo->exec("SET time_zone='$tz';");

回答by MNR

I can change my mysql default timezone from variable section by editing row which says "time zone" in phpmyadmin.

我可以通过编辑 phpmyadmin 中显示“时区”的行来从变量部分更改我的 mysql 默认时区。

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.

在这里,您还可以更改格式以及更多内容,您可以在 mysql 支持http://dev.mysql.com/doc/refman/5.7/en/time-zone-support.html 中找到,希望对您有所帮助。

enter image description here

在此处输入图片说明

You can put same timezone for both php and mysql.

您可以为 php 和 mysql 设置相同的时区。

回答by Aalphin

For PHP use this function:

对于 PHP,请使用此函数:

date_default_timezone_set()

MySQL:

MySQL:

default-time-zone='timezone'

If you have the rootprivilege, you can set the global server time zone value at run-time with this statement:

如果您具有root权限,则可以在运行时使用以下语句设置全局服务器时区值:

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_zonevariable, but the client can change its own time zone with this statement:

每个连接的时区。每个连接的客户端都有自己的时区设置,由 session 给出time_zone variable。最初,会话变量从变量中获取其值global time_zone,但客户端可以使用以下语句更改自己的时区:

SET time_zone = timezone;

回答by Clary

The best method Set timezone in PDO MySQL:

在 PDO MySQL 中设置时区的最佳方法:

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

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

回答by Clary

The best method Set timezone in PDO MySQL:

在 PDO MySQL 中设置时区的最佳方法:

If appropriate, whether by mistake you can use: date('P') Example:

如果合适,是否可以错误地使用: date('P') 示例:

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