Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mysql time and php time not the same

When I use current_timestamp with MySQL, I get the correct time, but when I use

$mysqldate = date( 'Y-m-d H:i:s' );

I get the date with with an hour delay (eg, 4:42 PM is 5:42 PM). I understood that both function use the server's local time - can someone explain the difference?

Thanks.

like image 793
daniel Avatar asked Jun 09 '09 12:06

daniel


2 Answers

The global system time will be set via the /etc/localtime file, which will be either a symlink or a copy of a zone file from /usr/share/zoneinfo/ on most systems. Applications will use this as their default.

PHP can override this in a couple different ways:

  1. date.timezone in php.ini
  2. setting the TZ environment variable, e.g. putenv("TZ=US/Central");
  3. date_default_timezone_set function (in PHP >= 5.1.0)

MySQL can override this by running the following query immediately after connecting:

SET time_zone = 'US/Central'
like image 184
Gabe Martin-Dempesy Avatar answered Oct 31 '22 22:10

Gabe Martin-Dempesy


There are php locale settings, it takes it from php.ini, not from system time

like image 21
Svetlozar Angelov Avatar answered Oct 31 '22 23:10

Svetlozar Angelov