8

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.

1
  • 1
    You're certain your MySQL and PHP are on the same server, right? Some hosts put the MySQL on a different server. Commented Jun 9, 2009 at 12:38

3 Answers 3

9

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'
Sign up to request clarification or add additional context in comments.

Comments

2

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

Comments

1

Maybe your PHP server thinks it's in a different time zone or it uses different locale and daylight saving time rules.

1 Comment

how do i get PHP to give me the default, server time that's listed on server's computer? PHP's date.timezone isn't set

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.