From dd6f32b3b506d29da38f41d307e5ea217c21cb51 Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Wed, 4 Jul 2012 10:08:54 +0300 Subject: Optimize standard_date() --- system/helpers/date_helper.php | 28 +++++++++++++--------------- 1 file changed, 13 insertions(+), 15 deletions(-) (limited to 'system/helpers') diff --git a/system/helpers/date_helper.php b/system/helpers/date_helper.php index 9637e26ce..6686089c6 100644 --- a/system/helpers/date_helper.php +++ b/system/helpers/date_helper.php @@ -121,22 +121,20 @@ if ( ! function_exists('standard_date')) * @param int Unix timestamp * @return string */ - function standard_date($fmt = 'DATE_RFC822', $time = '') + function standard_date($fmt = 'DATE_RFC822', $time = NULL) { - $formats = array( - 'DATE_ATOM' => '%Y-%m-%dT%H:%i:%s%P', - 'DATE_COOKIE' => '%l, %d-%M-%y %H:%i:%s UTC', - 'DATE_ISO8601' => '%Y-%m-%dT%H:%i:%s%P', - 'DATE_RFC822' => '%D, %d %M %y %H:%i:%s %O', - 'DATE_RFC850' => '%l, %d-%M-%y %H:%i:%s UTC', - 'DATE_RFC1036' => '%D, %d %M %y %H:%i:%s %O', - 'DATE_RFC1123' => '%D, %d %M %Y %H:%i:%s %O', - 'DATE_RFC2822' => '%r', - 'DATE_RSS' => '%D, %d %M %Y %H:%i:%s %O', - 'DATE_W3C' => '%Y-%m-%dT%H:%i:%s%P' - ); - - return isset($formats[$fmt]) ? mdate($formats[$fmt], $time) : FALSE; + if (empty($time)) + { + $time = now(); + } + + // Procedural style pre-defined constants from the DateTime extension + if (strpos($fmt, 'DATE_') !== 0 OR defined($fmt) === FALSE) + { + return FALSE; + } + + return date(constant($fmt), $time); } } -- cgit v1.2.3-24-g4f1b From ac57033236d5b62ba553b6bea87623f0dbeb2f49 Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Wed, 4 Jul 2012 13:04:10 +0300 Subject: Deprecate Date helper standard_date() --- system/helpers/date_helper.php | 13 +++++++++++++ 1 file changed, 13 insertions(+) (limited to 'system/helpers') diff --git a/system/helpers/date_helper.php b/system/helpers/date_helper.php index 6686089c6..a45b3d7ac 100644 --- a/system/helpers/date_helper.php +++ b/system/helpers/date_helper.php @@ -117,6 +117,19 @@ if ( ! function_exists('standard_date')) * * Returns a date formatted according to the submitted standard. * + * As of PHP 5.2, the DateTime extension provides constants that + * serve for the exact same purpose and are used with date(). + * Due to that, this function is DEPRECATED and should be removed + * in CodeIgniter 3.1+. + * + * Here are two examples of how you should replace it: + * + * date(DATE_RFC822, now()); // default + * date(DATE_W3C, $time); // a different format and time + * + * Reference: http://www.php.net/manual/en/class.datetime.php#datetime.constants.types + * + * @deprecated * @param string the chosen format * @param int Unix timestamp * @return string -- cgit v1.2.3-24-g4f1b From 4d1a214836fc16bbecd5afd11060a56bc2859b7d Mon Sep 17 00:00:00 2001 From: Phil Sturgeon Date: Wed, 4 Jul 2012 13:45:46 +0200 Subject: Having `"0"` in an array would give you NULL with `elements()`. BAD! The trouble here, is that when we are expecting a valid result, such as `0`, or `false` that the array helper will instead say "I don't know if there is a value" and give you a `null`. --- system/helpers/array_helper.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'system/helpers') diff --git a/system/helpers/array_helper.php b/system/helpers/array_helper.php index 5d0243951..0dfc3e1ae 100644 --- a/system/helpers/array_helper.php +++ b/system/helpers/array_helper.php @@ -52,7 +52,7 @@ if ( ! function_exists('element')) */ function element($item, $array, $default = NULL) { - return empty($array[$item]) ? $default : $array[$item]; + return ! isset($array[$item]) ? $default : $array[$item]; } } -- cgit v1.2.3-24-g4f1b From bf0e38be236812656890223eea3b9f0dc68582c0 Mon Sep 17 00:00:00 2001 From: Phil Sturgeon Date: Wed, 4 Jul 2012 14:42:29 +0200 Subject: Further update to `element()` to remove more unexpected results. --- system/helpers/array_helper.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'system/helpers') diff --git a/system/helpers/array_helper.php b/system/helpers/array_helper.php index 0dfc3e1ae..5a7f633e8 100644 --- a/system/helpers/array_helper.php +++ b/system/helpers/array_helper.php @@ -52,7 +52,7 @@ if ( ! function_exists('element')) */ function element($item, $array, $default = NULL) { - return ! isset($array[$item]) ? $default : $array[$item]; + return array_key_exists($array[$item]) ? $array[$item] : $default; } } @@ -95,7 +95,7 @@ if ( ! function_exists('elements')) foreach ($items as $item) { - $return[$item] = isset($array[$item]) ? $array[$item] : $default; + $return[$item] = array_key_exists($array[$item]) ? $array[$item] : $default; } return $return; -- cgit v1.2.3-24-g4f1b From 6801d044d7f7b905e1fdaf21231f8a3fad5b4057 Mon Sep 17 00:00:00 2001 From: Phil Sturgeon Date: Wed, 4 Jul 2012 14:43:36 +0200 Subject: Ahem. --- system/helpers/array_helper.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'system/helpers') diff --git a/system/helpers/array_helper.php b/system/helpers/array_helper.php index 5a7f633e8..ed2fe3c4a 100644 --- a/system/helpers/array_helper.php +++ b/system/helpers/array_helper.php @@ -52,7 +52,7 @@ if ( ! function_exists('element')) */ function element($item, $array, $default = NULL) { - return array_key_exists($array[$item]) ? $array[$item] : $default; + return array_key_exists($item, $array) ? $array[$item] : $default; } } @@ -95,7 +95,7 @@ if ( ! function_exists('elements')) foreach ($items as $item) { - $return[$item] = array_key_exists($array[$item]) ? $array[$item] : $default; + $return[$item] = array_key_exists($item, $array) ? $array[$item] : $default; } return $return; -- cgit v1.2.3-24-g4f1b