get server time in php - timezone issues

前端 未结 4 1444
攒了一身酷
攒了一身酷 2020-12-13 20:41

on shell, my server time is (simple date in bash): Sun Aug 29 10:37:12 EDT 2010

when I run the code php -r \"echo date(\'Y-m-d H:i:s\');\"

相关标签:
4条回答
  • 2020-12-13 21:17

    According to the php.ini directives manual page, the timezone isn't set by default if you're using a version prior to 5.3. Then if you take a look at the page for date_default_timezone_get, it uses the following order for getting the timezone:

    * Reading the timezone set using the date_default_timezone_set() function (if any)
    * Reading the TZ environment variable (if non empty) (Prior to PHP 5.3.0)
    * Reading the value of the date.timezone ini option (if set)
    * Querying the host operating system (if supported and allowed by the OS)
    

    UTC is the default if all of those fail, so that's probably a good starting point.

    0 讨论(0)
  • 2020-12-13 21:31

    The server should always have a timezone specified in the php.ini file. PHP 5.3 even raises notices if that's not the case and the DateTime extension will throw exceptions if you attempt to build objects without explicitly specifying a timezone (with a DateTimezone object) or having a default one (either through date.timezone or date_default_timezone_set).

    By what you're describing, the most likely scenarios are:

    • Different php.ini files are being used in the two scenarios. In the first case, no default timezone is specified, so PHP attempts to infer one from the system-wide timezone of the machine.
    • The (second) script calls date_default_timezone_set('UTC') or equivalent.
    0 讨论(0)
  • 2020-12-13 21:38

    Late for the party, but if you want to set the timezone without edit php.ini, you can put in the php code one of these strings:

    date_default_timezone_set('GMT');
    date_default_timezone_set('Europe/Zurich');
    date_default_timezone_set('America/New_York');
    
    0 讨论(0)
  • 2020-12-13 21:44

    Have you tried using date_default_timezone_set() American timezones can be found here http://www.php.net/manual/en/timezones.america.php

    0 讨论(0)
提交回复
热议问题