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.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…