JavaScript date_default_timezone_set
Sets the default timezone used by all date/time functions in a script
1 2 3 4 56 7 8 9 1011 12 13 14 1516 17 18 19 2021 22 23 24 2526 27 28 29 | function date_default_timezone_set (tz) { // Sets the default timezone used by all date/time functions in a script // // version: 1109.2015 // discuss at: http://phpjs.org/functions/date_default_timezone_set // + original by: Brett Zamir (http://brett-zamir.me) // - depends on: timezone_abbreviations_list // % note 1: Uses global: php_js to store the default timezone // * example 1: date_default_timezone_set('unknown'); // * returns 1: 'unknown' var tal = {}, abbr = '', i = 0; // BEGIN REDUNDANT this.php_js = this.php_js || {}; // END REDUNDANT // PHP verifies that the timezone is valid and also sets this.php_js.currentTimezoneOffset and this.php_js.currentTimezoneDST if so tal = this.timezone_abbreviations_list(); for (abbr in tal) { for (i = 0; i < tal[abbr].length; i++) { if (tal[abbr][i].timezone_id === tz) { this.php_js.default_timezone = tz; return true; } } } return false; } |
Examples
Running
1 | date_default_timezone_set('unknown'); |
Should return
1 | 'unknown' |
Dependencies
In order to use this function, you also need:
Open syntax issues
php.js uses JsLint to help us keep our code consistent and prevent some common bugs.
Eventually we want all code to pass or at least take into consideration most fixes suggested by JsLint, following this JsLint configuration we’ve decided on.
Authors
Thanks to the following developers, you get to have date_default_timezone_set goodness in JavaScript.
@Alberto Ruiz: You really should try posting in a help forum like StackOverflow (tagged with "PHP"), as this page is just about our JavaScript implementation. Try using "var_dump" instead of "echo" to ensure that the string is of the right length and not containing extra characters or something.
Can you pass a variable into date_default_timezone_set()?
$myTimeZONEvar=$_SESSION['setting_timezone_session']; // I saved 'America/New_York' into a session echo $myTimeZONEvar; // This shows that the variable is exactly 'America/New_York' date_default_timezone_set($myTimeZONEvar); // I get an error everytime I try to run this...but if I remove the variable and put in the string 'America/New_York' ....then it works.


Alberto Basso
7 Dec '11
Tested on Firefox 3.6.24, but also on more recent version 7.0.1.
Thanks very much for your work!
Bye!