From 8310595f0e80f72ac790478b8a7dfc6470051639 Mon Sep 17 00:00:00 2001 From: Iban Eguia Date: Tue, 27 Mar 2012 18:18:15 +0200 Subject: Changed Date helper to return time() based on the timezone parameter. --- application/config/config.php | 16 +++++++--------- index.php | 2 ++ system/helpers/date_helper.php | 24 +++++++++--------------- 3 files changed, 18 insertions(+), 24 deletions(-) diff --git a/application/config/config.php b/application/config/config.php index 2628885f0..4d0e5080a 100644 --- a/application/config/config.php +++ b/application/config/config.php @@ -204,7 +204,7 @@ $config['directory_trigger'] = 'd'; // experimental not currently in use | 4 = All Messages | | You can also pass in a array with threshold levels to show individual error types -| +| | array(2) = Debug Messages, without Error Messages | | For a live site you'll usually only enable Errors (1) to be logged otherwise @@ -253,7 +253,7 @@ $config['cache_path'] = ''; | | If you use the Encryption class or the Session class you | MUST set an encryption key. See the user guide for info. -| +| | http://codeigniter.com/user_guide/libraries/encryption.html | http://codeigniter.com/user_guide/libraries/sessions.html | @@ -297,7 +297,7 @@ $config['sess_time_to_update'] = 300; | 'cookie_domain' = Set to .your-domain.com for site-wide cookies | 'cookie_path' = Typically will be a forward slash | 'cookie_secure' = Cookies will only be set if a secure HTTPS connection exists. -| 'cookie_httponly' = Cookie will only be accessible via HTTP(S) (no javascript) +| 'cookie_httponly' = Cookie will only be accessible via HTTP(S) (no javascript) | */ $config['cookie_prefix'] = ""; @@ -359,16 +359,14 @@ $config['compress_output'] = FALSE; /* |-------------------------------------------------------------------------- -| Master Time Reference +| Master Timezone |-------------------------------------------------------------------------- | -| Options are 'local' or 'gmt'. This pref tells the system whether to use -| your server's local time as the master 'now' reference, or convert it to -| GMT. See the 'date helper' page of the user guide for information -| regarding date handling. +| You can set any PHP supported timezones to be the master timezone when +| you call th now() function. | */ -$config['time_reference'] = 'local'; +$config['timezone'] = 'UTC'; /* diff --git a/index.php b/index.php index 5a1190112..28586093d 100644 --- a/index.php +++ b/index.php @@ -162,6 +162,8 @@ if (defined('ENVIRONMENT')) // END OF USER CONFIGURABLE SETTINGS. DO NOT EDIT BELOW THIS LINE // -------------------------------------------------------------------- +date_default_timezone_set('UTC'); + /* * --------------------------------------------------------------- * Resolve the system path for increased reliability diff --git a/system/helpers/date_helper.php b/system/helpers/date_helper.php index f1ba364f5..aecc7d90f 100644 --- a/system/helpers/date_helper.php +++ b/system/helpers/date_helper.php @@ -46,25 +46,19 @@ */ if ( ! function_exists('now')) { - function now() + function now($timezone = NULL) { - $CI =& get_instance(); + $CI =& get_instance(); - if (strtolower($CI->config->item('time_reference')) == 'gmt') - { - $now = time(); - $system_time = mktime(gmdate("H", $now), gmdate("i", $now), gmdate("s", $now), gmdate("m", $now), gmdate("d", $now), gmdate("Y", $now)); + if (is_null($timezone)) + $timezone = $CI->config->item('timezone'); - if (strlen($system_time) < 10) - { - $system_time = time(); - log_message('error', 'The Date class could not set a proper GMT timestamp so the local time() value was used.'); - } + $timezone = new DateTimeZone($timezone); + $now = new DateTime('now', $timezone); + $offset = $timezone->getOffset($now); + $time = time() + $offset; - return $system_time; - } - - return time(); + return $time; } } -- cgit v1.2.3-24-g4f1b From 7bf0a4ff35efc758ef43b3a848e655285946b8b6 Mon Sep 17 00:00:00 2001 From: Iban Eguia Date: Tue, 27 Mar 2012 18:36:15 +0200 Subject: Added doccumentation for the new date helper. --- system/helpers/date_helper.php | 4 +++- user_guide_src/source/changelog.rst | 1 + user_guide_src/source/helpers/date_helper.rst | 22 ++++++++++++++-------- user_guide_src/source/installation/upgrade_300.rst | 6 ++++++ 4 files changed, 24 insertions(+), 9 deletions(-) diff --git a/system/helpers/date_helper.php b/system/helpers/date_helper.php index aecc7d90f..7ff7444e5 100644 --- a/system/helpers/date_helper.php +++ b/system/helpers/date_helper.php @@ -40,8 +40,10 @@ /** * Get "now" time * - * Returns time() or its GMT equivalent based on the config file preference + * Returns time() based on the timezone parameter or on the "timezone" + * setting * + * @param string * @return int */ if ( ! function_exists('now')) diff --git a/user_guide_src/source/changelog.rst b/user_guide_src/source/changelog.rst index b4bf0cfaa..e5b6eea47 100644 --- a/user_guide_src/source/changelog.rst +++ b/user_guide_src/source/changelog.rst @@ -40,6 +40,7 @@ Release Date: Not Released - Helpers + - Date helper will now return now() based on the timezone you specify. - url_title() will now trim extra dashes from beginning and end. - Added XHTML Basic 1.1 doctype to :doc:`HTML Helper `. - Changed humanize to include a second param for the separator. diff --git a/user_guide_src/source/helpers/date_helper.rst b/user_guide_src/source/helpers/date_helper.rst index b21d147bd..b8c3dd076 100644 --- a/user_guide_src/source/helpers/date_helper.rst +++ b/user_guide_src/source/helpers/date_helper.rst @@ -20,14 +20,20 @@ The following functions are available: now() ===== -Returns the current time as a Unix timestamp, referenced either to your -server's local time or GMT, based on the "time reference" setting in -your config file. If you do not intend to set your master time reference -to GMT (which you'll typically do if you run a site that lets each user -set their own timezone settings) there is no benefit to using this -function over PHP's time() function. - -.. php:method:: now() +Returns the current time as a Unix timestamp, based on the "timezone" parameter. +All PHP available timezones are supported. + +.. php:method:: now($timezone = NULL) + + :param string $timezone: The timezone you want to be returned + :returns: integer + +:: + + $tz = "Australia/Victoria"; + echo now($tz); + +If a timezone is not provided, it will return time() based on "timezone" setting. mdate() ======= diff --git a/user_guide_src/source/installation/upgrade_300.rst b/user_guide_src/source/installation/upgrade_300.rst index 4c594ab17..b6f52080a 100644 --- a/user_guide_src/source/installation/upgrade_300.rst +++ b/user_guide_src/source/installation/upgrade_300.rst @@ -31,3 +31,9 @@ Step 3: Remove $autoload['core'] from your config/autoload.php Use of the `$autoload['core']` config array has been deprecated as of CodeIgniter 1.4.1 and is now removed. Move any entries that you might have listed there to `$autoload['libraries']` instead. + +Step 4: Change your use of the Date heper's now() function +========================================================== + +Function now() has been modified. You can see the changes in :doc:`Date Helper <../helpers/date_helper>` +You must replace $config['time_reference'] with $config['timezone']. \ No newline at end of file -- cgit v1.2.3-24-g4f1b From 91a13199292d8b4495e6ecbcb6a5fea1294cd2da Mon Sep 17 00:00:00 2001 From: Iban Eguia Date: Tue, 27 Mar 2012 18:50:32 +0200 Subject: Fixed typo. --- application/config/config.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/application/config/config.php b/application/config/config.php index 4d0e5080a..eb3ddddb0 100644 --- a/application/config/config.php +++ b/application/config/config.php @@ -363,7 +363,7 @@ $config['compress_output'] = FALSE; |-------------------------------------------------------------------------- | | You can set any PHP supported timezones to be the master timezone when -| you call th now() function. +| you call the now() function. | */ $config['timezone'] = 'UTC'; -- cgit v1.2.3-24-g4f1b From 16760bb0d812b951564bd1742af6e622490ca05c Mon Sep 17 00:00:00 2001 From: Iban Eguia Date: Tue, 27 Mar 2012 18:51:52 +0200 Subject: Added braces as requested. --- system/helpers/date_helper.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/system/helpers/date_helper.php b/system/helpers/date_helper.php index 7ff7444e5..14d973f65 100644 --- a/system/helpers/date_helper.php +++ b/system/helpers/date_helper.php @@ -53,7 +53,9 @@ if ( ! function_exists('now')) $CI =& get_instance(); if (is_null($timezone)) + { $timezone = $CI->config->item('timezone'); + } $timezone = new DateTimeZone($timezone); $now = new DateTime('now', $timezone); -- cgit v1.2.3-24-g4f1b From 0ed4f63f4268b0c98f549ffd711702fd45a761d0 Mon Sep 17 00:00:00 2001 From: Iban Eguia Date: Tue, 27 Mar 2012 22:07:44 +0200 Subject: Fixed a typo and added the route for the config file. --- user_guide_src/source/installation/upgrade_300.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/user_guide_src/source/installation/upgrade_300.rst b/user_guide_src/source/installation/upgrade_300.rst index b6f52080a..5d6450ee6 100644 --- a/user_guide_src/source/installation/upgrade_300.rst +++ b/user_guide_src/source/installation/upgrade_300.rst @@ -32,8 +32,8 @@ Step 3: Remove $autoload['core'] from your config/autoload.php Use of the `$autoload['core']` config array has been deprecated as of CodeIgniter 1.4.1 and is now removed. Move any entries that you might have listed there to `$autoload['libraries']` instead. -Step 4: Change your use of the Date heper's now() function +Step 4: Change your use of the Date helper's now() function ========================================================== Function now() has been modified. You can see the changes in :doc:`Date Helper <../helpers/date_helper>` -You must replace $config['time_reference'] with $config['timezone']. \ No newline at end of file +You must replace $config['time_reference'] with $config['timezone'] in your config.php file. \ No newline at end of file -- cgit v1.2.3-24-g4f1b From f5d1fd2ce8bfa049c8049184ac6d385d0f70fe29 Mon Sep 17 00:00:00 2001 From: Iban Eguia Date: Fri, 8 Jun 2012 23:37:33 +0200 Subject: Updated the upgrade guide to specify which are the supported timezones. --- user_guide_src/source/installation/upgrade_300.rst | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/user_guide_src/source/installation/upgrade_300.rst b/user_guide_src/source/installation/upgrade_300.rst index 503e5366c..e86ef67da 100644 --- a/user_guide_src/source/installation/upgrade_300.rst +++ b/user_guide_src/source/installation/upgrade_300.rst @@ -46,7 +46,8 @@ Step 5: Change your use of the Date helper's now() function =========================================================== Function now() has been modified. You can see the changes in :doc:`Date Helper <../helpers/date_helper>` -You must replace $config['time_reference'] with $config['timezone'] in your config.php file. +You must replace $config['time_reference'] with $config['timezone'] in your config.php file. You can select all +PHP supported timezones, listed here: `PHP's supported timezones `_. Step 6: Move your errors folder =============================== -- cgit v1.2.3-24-g4f1b From e15e3dde9dca15e2d65f098010d3fb7004cef5e7 Mon Sep 17 00:00:00 2001 From: Iban Eguia Date: Sat, 9 Jun 2012 23:52:27 +0200 Subject: Fixed timezone change in index.php Now it does not ever change the local timezone, and it adds the option to get the 'local' time() --- index.php | 2 -- system/helpers/date_helper.php | 14 ++++++++++---- user_guide_src/source/helpers/date_helper.rst | 3 ++- user_guide_src/source/installation/upgrade_300.rst | 3 ++- 4 files changed, 14 insertions(+), 8 deletions(-) diff --git a/index.php b/index.php index 62fc2ab1b..3b00dd360 100644 --- a/index.php +++ b/index.php @@ -162,8 +162,6 @@ if (defined('ENVIRONMENT')) // END OF USER CONFIGURABLE SETTINGS. DO NOT EDIT BELOW THIS LINE // -------------------------------------------------------------------- -date_default_timezone_set('UTC'); - /* * --------------------------------------------------------------- * Resolve the system path for increased reliability diff --git a/system/helpers/date_helper.php b/system/helpers/date_helper.php index 3b0c3289d..d5acec23f 100644 --- a/system/helpers/date_helper.php +++ b/system/helpers/date_helper.php @@ -57,10 +57,16 @@ if ( ! function_exists('now')) $timezone = $CI->config->item('timezone'); } - $timezone = new DateTimeZone($timezone); - $now = new DateTime('now', $timezone); - $offset = $timezone->getOffset($now); - $time = time() + $offset; + $time = time(); + if(strtolower($timezone) != 'local') + { + $local = new DateTime(NULL, new DateTimeZone(date_default_timezone_get())); + $now = new DateTime(NULL, new DateTimeZone($timezone)); + $lcl_offset = $local->getOffset(); + $tz_offset = $now->getOffset(); + $offset = $tz_offset - $lcl_offset; + $time = $time + $offset; + } return $time; } diff --git a/user_guide_src/source/helpers/date_helper.rst b/user_guide_src/source/helpers/date_helper.rst index b6c6ed4bb..33b39bd5b 100644 --- a/user_guide_src/source/helpers/date_helper.rst +++ b/user_guide_src/source/helpers/date_helper.rst @@ -21,7 +21,8 @@ now() ===== Returns the current time as a Unix timestamp, based on the "timezone" parameter. -All PHP available timezones are supported. +All PHP available timezones are supported. You can also use 'local' timezone, and +it will return time(). .. php:method:: now($timezone = NULL) diff --git a/user_guide_src/source/installation/upgrade_300.rst b/user_guide_src/source/installation/upgrade_300.rst index e86ef67da..c2c3899ee 100644 --- a/user_guide_src/source/installation/upgrade_300.rst +++ b/user_guide_src/source/installation/upgrade_300.rst @@ -47,7 +47,8 @@ Step 5: Change your use of the Date helper's now() function Function now() has been modified. You can see the changes in :doc:`Date Helper <../helpers/date_helper>` You must replace $config['time_reference'] with $config['timezone'] in your config.php file. You can select all -PHP supported timezones, listed here: `PHP's supported timezones `_. +PHP supported timezones, listed here: `Supported timezones `_. You can also +use 'local' if you want to get time(). Step 6: Move your errors folder =============================== -- cgit v1.2.3-24-g4f1b From a9617a35ce4af051d3ad1298c2c24453460754cc Mon Sep 17 00:00:00 2001 From: Iban Eguia Date: Sun, 10 Jun 2012 00:13:04 +0200 Subject: Changed the default timezone to local and explained in the config file. --- application/config/config.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/application/config/config.php b/application/config/config.php index eb3ddddb0..12ef77c76 100644 --- a/application/config/config.php +++ b/application/config/config.php @@ -363,10 +363,11 @@ $config['compress_output'] = FALSE; |-------------------------------------------------------------------------- | | You can set any PHP supported timezones to be the master timezone when -| you call the now() function. +| you call the now() function. 'local' string can be used to get the local +| time. | */ -$config['timezone'] = 'UTC'; +$config['timezone'] = 'local'; /* -- cgit v1.2.3-24-g4f1b From c88daba688d309150a7dce43817ab76ec7834bda Mon Sep 17 00:00:00 2001 From: Iban Eguia Date: Mon, 11 Jun 2012 13:58:30 +0200 Subject: Optimized now() function. Thanks to @narfbg --- system/helpers/date_helper.php | 21 ++++++++------------- 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/system/helpers/date_helper.php b/system/helpers/date_helper.php index d5acec23f..b818da9d8 100644 --- a/system/helpers/date_helper.php +++ b/system/helpers/date_helper.php @@ -50,25 +50,20 @@ if ( ! function_exists('now')) */ function now($timezone = NULL) { - $CI =& get_instance(); - - if (is_null($timezone)) + if (empty($timezone)) { - $timezone = $CI->config->item('timezone'); + $timezone = config_item('timezone'); } - $time = time(); - if(strtolower($timezone) != 'local') + if ($timezone === 'local' OR $timezone === date_default_timezone_get()) { - $local = new DateTime(NULL, new DateTimeZone(date_default_timezone_get())); - $now = new DateTime(NULL, new DateTimeZone($timezone)); - $lcl_offset = $local->getOffset(); - $tz_offset = $now->getOffset(); - $offset = $tz_offset - $lcl_offset; - $time = $time + $offset; + return time(); } - return $time; + $datetime = new DateTime('now', new DateTimeZone($timezone)); + sscanf($datetime->format('j-n-Y G:i:s'), '%d-%d-%d %d:%d:%d', $day, $month, $year, $hour, $minute, $second); + + return mktime($hour, $minute, $second, $month, $day, $year); } } -- cgit v1.2.3-24-g4f1b From feb14dac4e7a417a48344a5188a8ad8074871df4 Mon Sep 17 00:00:00 2001 From: Iban Eguia Date: Tue, 12 Jun 2012 16:09:36 +0200 Subject: Changed the config parameter. The session's _get_time() function has also changed. --- application/config/config.php | 4 ++-- system/helpers/date_helper.php | 2 +- system/libraries/Session.php | 14 +++++++++++--- user_guide_src/source/helpers/date_helper.rst | 6 ++---- user_guide_src/source/installation/upgrade_300.rst | 5 ++--- 5 files changed, 18 insertions(+), 13 deletions(-) diff --git a/application/config/config.php b/application/config/config.php index 12ef77c76..fd6a1aeb0 100644 --- a/application/config/config.php +++ b/application/config/config.php @@ -359,7 +359,7 @@ $config['compress_output'] = FALSE; /* |-------------------------------------------------------------------------- -| Master Timezone +| Master Time Reference |-------------------------------------------------------------------------- | | You can set any PHP supported timezones to be the master timezone when @@ -367,7 +367,7 @@ $config['compress_output'] = FALSE; | time. | */ -$config['timezone'] = 'local'; +$config['time_reference'] = 'local'; /* diff --git a/system/helpers/date_helper.php b/system/helpers/date_helper.php index b818da9d8..131508f69 100644 --- a/system/helpers/date_helper.php +++ b/system/helpers/date_helper.php @@ -52,7 +52,7 @@ if ( ! function_exists('now')) { if (empty($timezone)) { - $timezone = config_item('timezone'); + $timezone = config_item('time_reference'); } if ($timezone === 'local' OR $timezone === date_default_timezone_get()) diff --git a/system/libraries/Session.php b/system/libraries/Session.php index 7beedd96b..9fdf744c3 100644 --- a/system/libraries/Session.php +++ b/system/libraries/Session.php @@ -786,9 +786,17 @@ class CI_Session { */ protected function _get_time() { - return (strtolower($this->time_reference) === 'gmt') - ? mktime(gmdate('H'), gmdate('i'), gmdate('s'), gmdate('m'), gmdate('d'), gmdate('Y')) - : time(); + $timezone = config_item('time_reference'); + + if ($timezone === 'local' OR $timezone === date_default_timezone_get()) + { + return time(); + } + + $datetime = new DateTime('now', new DateTimeZone($timezone)); + sscanf($datetime->format('j-n-Y G:i:s'), '%d-%d-%d %d:%d:%d', $day, $month, $year, $hour, $minute, $second); + + return mktime($hour, $minute, $second, $month, $day, $year); } // -------------------------------------------------------------------- diff --git a/user_guide_src/source/helpers/date_helper.rst b/user_guide_src/source/helpers/date_helper.rst index 33b39bd5b..7bbfd4f15 100644 --- a/user_guide_src/source/helpers/date_helper.rst +++ b/user_guide_src/source/helpers/date_helper.rst @@ -30,11 +30,9 @@ it will return time(). :returns: integer :: + echo now("Australia/Victoria"); - $tz = "Australia/Victoria"; - echo now($tz); - -If a timezone is not provided, it will return time() based on "timezone" setting. +If a timezone is not provided, it will return time() based on "time_reference" setting. mdate() ======= diff --git a/user_guide_src/source/installation/upgrade_300.rst b/user_guide_src/source/installation/upgrade_300.rst index c2c3899ee..debf9662c 100644 --- a/user_guide_src/source/installation/upgrade_300.rst +++ b/user_guide_src/source/installation/upgrade_300.rst @@ -45,9 +45,8 @@ need to rename the `$active_record` variable to `$query_builder`. Step 5: Change your use of the Date helper's now() function =========================================================== -Function now() has been modified. You can see the changes in :doc:`Date Helper <../helpers/date_helper>` -You must replace $config['time_reference'] with $config['timezone'] in your config.php file. You can select all -PHP supported timezones, listed here: `Supported timezones `_. You can also +Function now() has been modified. You can see the changes in :doc:`Date Helper <../helpers/date_helper>`. +You can now select all PHP supported timezones in the `time_reference` setting, listed here: `Supported timezones `_. You can also use 'local' if you want to get time(). Step 6: Move your errors folder -- cgit v1.2.3-24-g4f1b From 7400965017f87c3aba18bf75ed7d732359fd577d Mon Sep 17 00:00:00 2001 From: Iban Eguia Date: Wed, 13 Jun 2012 22:57:50 +0200 Subject: Fixed some stuff in documentation. --- application/config/config.php | 7 ++++--- system/helpers/date_helper.php | 4 ++-- user_guide_src/source/helpers/date_helper.rst | 9 ++++++--- user_guide_src/source/installation/upgrade_300.rst | 4 ++-- 4 files changed, 14 insertions(+), 10 deletions(-) diff --git a/application/config/config.php b/application/config/config.php index fd6a1aeb0..31ff2024d 100644 --- a/application/config/config.php +++ b/application/config/config.php @@ -362,9 +362,10 @@ $config['compress_output'] = FALSE; | Master Time Reference |-------------------------------------------------------------------------- | -| You can set any PHP supported timezones to be the master timezone when -| you call the now() function. 'local' string can be used to get the local -| time. +| Options are 'local' or any PHP supported timezone. This pref tells the +| system whether to use your server's local time as the master 'now' +| reference, or convert it to any PHP supported timezone. See the 'date +| helper' page of the user guide for information regarding date handling. | */ $config['time_reference'] = 'local'; diff --git a/system/helpers/date_helper.php b/system/helpers/date_helper.php index 131508f69..a5c46e47b 100644 --- a/system/helpers/date_helper.php +++ b/system/helpers/date_helper.php @@ -42,8 +42,8 @@ if ( ! function_exists('now')) /** * Get "now" time * - * Returns time() based on the timezone parameter or on the "timezone" - * setting + * Returns time() based on the timezone parameter or on the + * "time_reference" setting * * @param string * @return int diff --git a/user_guide_src/source/helpers/date_helper.rst b/user_guide_src/source/helpers/date_helper.rst index 7bbfd4f15..1b7177fc2 100644 --- a/user_guide_src/source/helpers/date_helper.rst +++ b/user_guide_src/source/helpers/date_helper.rst @@ -20,9 +20,12 @@ The following functions are available: now() ===== -Returns the current time as a Unix timestamp, based on the "timezone" parameter. -All PHP available timezones are supported. You can also use 'local' timezone, and -it will return time(). +Returns the current time as a Unix timestamp, referenced either to your +server's local time or any PHP suported timezone, based on the "time reference" +setting in your config file. If you do not intend to set your master time reference +to any other PHP suported timezone (which you'll typically do if you run a site that +lets each user set their own timezone settings) there is no benefit to using this +function over PHP's time() function. .. php:method:: now($timezone = NULL) diff --git a/user_guide_src/source/installation/upgrade_300.rst b/user_guide_src/source/installation/upgrade_300.rst index debf9662c..d8a3d5bc1 100644 --- a/user_guide_src/source/installation/upgrade_300.rst +++ b/user_guide_src/source/installation/upgrade_300.rst @@ -46,8 +46,8 @@ Step 5: Change your use of the Date helper's now() function =========================================================== Function now() has been modified. You can see the changes in :doc:`Date Helper <../helpers/date_helper>`. -You can now select all PHP supported timezones in the `time_reference` setting, listed here: `Supported timezones `_. You can also -use 'local' if you want to get time(). +You can now select all PHP supported timezones in the `time_reference` setting, listed here: +`Supported timezones `_. You can also use 'local' if you want to get time(). Step 6: Move your errors folder =============================== -- cgit v1.2.3-24-g4f1b