Setting PHP's default timezone

笑着哭i 提交于 2019-12-20 06:31:08

问题


In my web app, I let the users choose their preferred timezone from a list (that contains all timezones that PHP supports).

Let's say that $_POST['timezone'] is the chosen timezone (e.g. America/New_York). I set it with the following code which produces no errors:

default_date_timezone_set($_POST['timezone']);

But when I reload the page it goes back to what it was before (e.g. Europe/Moscow). Do I have to set the default timezone in every script or isn't the function (default_date_timezone_set) working properly? Thanks!


回答1:


This function changes the timezone for the execution of the script only. You could store the timezone in a session variable and set the time zone on top of every page.




回答2:


You should save it in the database and set the timezone any time you do anything time related. It's working properly. It's only supposed to set it for that script execution (one HTTP request).




回答3:


Set a cookie that contains the timezone the user chooses.

/// Make sure you sanitize all POST/COOKIE variables if needed.
$timezone = $_COOKIE['timezone'];

if(isset($_POST['timezone'])) {
    /// Set cookie for some amount of time -- I chose 2 weeks
    setcookie('timezone',$_POST['timezone'],time()+60*60*24*14);
    $timezone = $_POST['timezone'];
}

default_date_timezone_set($timezone);


来源:https://stackoverflow.com/questions/9674237/setting-phps-default-timezone

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!