Setting PHP's default timezone

后端 未结 3 1587
离开以前
离开以前 2021-01-28 11:55

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\']

相关标签:
3条回答
  • 2021-01-28 12:26

    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.

    0 讨论(0)
  • 2021-01-28 12:30

    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).

    0 讨论(0)
  • 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);
    
    0 讨论(0)
提交回复
热议问题