Dealing with PHP server and MySQL server in different time zones

后端 未结 5 561
眼角桃花
眼角桃花 2020-12-13 11:30

For those of us who use standard shared hosting packages, such as GoDaddy or Network Solutions, how do you handle datetime conversions when your hosting server (PHP) and MyS

5条回答
  •  借酒劲吻你
    2020-12-13 11:48

    As of PHP 5.1.0 you can use date_default_timezone_set() function to set the default timezone used by all date/time functions in a script.

    For MySql (quoted from MySQL Server Time Zone Support page)

    Before MySQL 4.1.3, the server operates only in the system time zone set at startup. Beginning with MySQL 4.1.3, the server maintains several time zone settings, some of which can be modified at runtime.

    Of interest to you is per-connection setting of the time zones, which you would use at the beginning of your scripts

    SET timezone = 'Europe/London';
    

    As for detecting the client timezone setting, you could use a bit of JavaScript to get and save that information to a cookie, and use it on subsequent page reads, to calculate the proper timezone.

    //Returns the offset (time difference) between Greenwich Mean Time (GMT) 
    //and local time of Date object, in minutes.
    var offset = new Date().getTimezoneOffset(); 
    document.cookie = 'timezoneOffset=' + escape(offset);
    

    Or you could offer users the chioce to set their time zones themselves.

提交回复
热议问题