From 5d1e32b2fbae74e6f9e1ab2bdb6a3635579ef13e Mon Sep 17 00:00:00 2001 From: Eric Barnes Date: Tue, 3 May 2011 22:28:59 -0400 Subject: Added unit tests for date helper. --- system/helpers/date_helper.php | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) (limited to 'system/helpers/date_helper.php') diff --git a/system/helpers/date_helper.php b/system/helpers/date_helper.php index f3f01f751..951181b8c 100644 --- a/system/helpers/date_helper.php +++ b/system/helpers/date_helper.php @@ -112,15 +112,16 @@ if ( ! function_exists('standard_date')) function standard_date($fmt = 'DATE_RFC822', $time = '') { $formats = array( - 'DATE_ATOM' => '%Y-%m-%dT%H:%i:%s%Q', + 'DATE_ATOM' => '%Y-%m-%dT%H:%i:%s%O', 'DATE_COOKIE' => '%l, %d-%M-%y %H:%i:%s UTC', - 'DATE_ISO8601' => '%Y-%m-%dT%H:%i:%s%Q', + 'DATE_ISO8601' => '%Y-%m-%dT%H:%i:%s%O', 'DATE_RFC822' => '%D, %d %M %y %H:%i:%s %O', - 'DATE_RFC850' => '%l, %d-%M-%y %H:%m:%i UTC', + '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' => '%D, %d %M %Y %H:%i:%s %O', 'DATE_RSS' => '%D, %d %M %Y %H:%i:%s %O', - 'DATE_W3C' => '%Y-%m-%dT%H:%i:%s%Q' + 'DATE_W3C' => '%Y-%m-%dT%H:%i:%s%O' ); if ( ! isset($formats[$fmt])) -- cgit v1.2.3-24-g4f1b From 03dbcf0669abdddf6bb459a423b99e7aed73e453 Mon Sep 17 00:00:00 2001 From: Roger Herbert Date: Sun, 11 Mar 2012 11:48:50 +0000 Subject: Make timespan() helper show fuzzier periods if required --- system/helpers/date_helper.php | 131 +++++++++++++++++++++++++++-------------- 1 file changed, 86 insertions(+), 45 deletions(-) (limited to 'system/helpers/date_helper.php') diff --git a/system/helpers/date_helper.php b/system/helpers/date_helper.php index 2a34cf93e..9f8e05bb9 100644 --- a/system/helpers/date_helper.php +++ b/system/helpers/date_helper.php @@ -163,7 +163,7 @@ if ( ! function_exists('standard_date')) */ if ( ! function_exists('timespan')) { - function timespan($seconds = 1, $time = '') + function timespan($seconds = 1, $time = '', $units = '') { $CI =& get_instance(); $CI->lang->load('date'); @@ -178,83 +178,124 @@ if ( ! function_exists('timespan')) $time = time(); } - $seconds = ($time <= $seconds) ? 1 : $time - $seconds; + if ( ! is_numeric($units)) + { + $units = 999; + } + + if ($time <= $seconds) + { + $seconds = 1; + } + else + { + $seconds = $time - $seconds; + } - $str = ''; - $years = floor($seconds / 31557600); + $str = array(); + + // years + + $years = floor($seconds / 31536000); if ($years > 0) { - $str .= $years.' '.$CI->lang->line((($years > 1) ? 'date_years' : 'date_year')).', '; + $str[] = $years.' '.$CI->lang->line((($years > 1) ? 'date_years' : 'date_year')); } - $seconds -= $years * 31557600; - $months = floor($seconds / 2629743); + $seconds -= $years * 31536000; - if ($years > 0 OR $months > 0) - { - if ($months > 0) + // months + + if (count($str) < $units) { + $months = floor($seconds / 2628000); + + if ($years > 0 OR $months > 0) { - $str .= $months.' '.$CI->lang->line((($months > 1) ? 'date_months' : 'date_month')).', '; - } + if ($months > 0) + { + $str[] = $months.' '.$CI->lang->line((($months > 1) ? 'date_months' : 'date_month')); + } - $seconds -= $months * 2629743; + $seconds -= $months * 2628000; + } } - $weeks = floor($seconds / 604800); + // weeks + + if (count($str) < $units) { + $weeks = floor($seconds / 604800); - if ($years > 0 OR $months > 0 OR $weeks > 0) - { - if ($weeks > 0) + if ($years > 0 OR $months > 0 OR $weeks > 0) { - $str .= $weeks.' '.$CI->lang->line((($weeks > 1) ? 'date_weeks' : 'date_week')).', '; - } + if ($weeks > 0) + { + $str[] = $weeks.' '.$CI->lang->line((($weeks > 1) ? 'date_weeks' : 'date_week')); + } - $seconds -= $weeks * 604800; + $seconds -= $weeks * 604800; + } } - $days = floor($seconds / 86400); + // days - if ($months > 0 OR $weeks > 0 OR $days > 0) - { - if ($days > 0) + if (count($str) < $units) { + $days = floor($seconds / 86400); + + if ($months > 0 OR $weeks > 0 OR $days > 0) { - $str .= $days.' '.$CI->lang->line((($days > 1) ? 'date_days' : 'date_day')).', '; - } + if ($days > 0) + { + $str[] = $days.' '.$CI->lang->line((($days > 1) ? 'date_days' : 'date_day')); + } - $seconds -= $days * 86400; + $seconds -= $days * 86400; + } } - $hours = floor($seconds / 3600); + // hours - if ($days > 0 OR $hours > 0) - { - if ($hours > 0) + if (count($str) < $units) { + + $hours = floor($seconds / 3600); + + if ($days > 0 OR $hours > 0) { - $str .= $hours.' '.$CI->lang->line((($hours > 1) ? 'date_hours' : 'date_hour')).', '; - } + if ($hours > 0) + { + $str[] = $hours.' '.$CI->lang->line((($hours > 1) ? 'date_hours' : 'date_hour')); + } - $seconds -= $hours * 3600; + $seconds -= $hours * 3600; + } } - $minutes = floor($seconds / 60); + // minutes - if ($days > 0 OR $hours > 0 OR $minutes > 0) - { - if ($minutes > 0) + if (count($str) < $units) { + + $minutes = floor($seconds / 60); + + if ($days > 0 OR $hours > 0 OR $minutes > 0) { - $str .= $minutes.' '.$CI->lang->line((($minutes > 1) ? 'date_minutes' : 'date_minute')).', '; - } + if ($minutes > 0) + { + $str[] = $minutes.' '.$CI->lang->line((($minutes > 1) ? 'date_minutes' : 'date_minute')); + } - $seconds -= $minutes * 60; + $seconds -= $minutes * 60; + } } - if ($str == '') - { - $str .= $seconds.' '.$CI->lang->line((($seconds > 1) ? 'date_seconds' : 'date_second')).', '; + // seconds + + if (count($str) == 0) { + { + $str[] = $seconds.' '.$CI->lang->line((($seconds > 1) ? 'date_seconds' : 'date_second')); + } } - return substr(trim($str), 0, -1); + return strtolower(implode(', ', $str)); } } -- cgit v1.2.3-24-g4f1b From 11f46f736b93529e8310135669196dec98e94d90 Mon Sep 17 00:00:00 2001 From: Roger Herbert Date: Sun, 11 Mar 2012 15:11:44 +0000 Subject: Revert "Make timespan() helper show fuzzier periods if required" This reverts commit 03dbcf0669abdddf6bb459a423b99e7aed73e453. --- system/helpers/date_helper.php | 131 ++++++++++++++--------------------------- 1 file changed, 45 insertions(+), 86 deletions(-) (limited to 'system/helpers/date_helper.php') diff --git a/system/helpers/date_helper.php b/system/helpers/date_helper.php index 9f8e05bb9..2a34cf93e 100644 --- a/system/helpers/date_helper.php +++ b/system/helpers/date_helper.php @@ -163,7 +163,7 @@ if ( ! function_exists('standard_date')) */ if ( ! function_exists('timespan')) { - function timespan($seconds = 1, $time = '', $units = '') + function timespan($seconds = 1, $time = '') { $CI =& get_instance(); $CI->lang->load('date'); @@ -178,124 +178,83 @@ if ( ! function_exists('timespan')) $time = time(); } - if ( ! is_numeric($units)) - { - $units = 999; - } - - if ($time <= $seconds) - { - $seconds = 1; - } - else - { - $seconds = $time - $seconds; - } + $seconds = ($time <= $seconds) ? 1 : $time - $seconds; - $str = array(); - - // years - - $years = floor($seconds / 31536000); + $str = ''; + $years = floor($seconds / 31557600); if ($years > 0) { - $str[] = $years.' '.$CI->lang->line((($years > 1) ? 'date_years' : 'date_year')); + $str .= $years.' '.$CI->lang->line((($years > 1) ? 'date_years' : 'date_year')).', '; } - $seconds -= $years * 31536000; + $seconds -= $years * 31557600; + $months = floor($seconds / 2629743); - // months - - if (count($str) < $units) { - $months = floor($seconds / 2628000); - - if ($years > 0 OR $months > 0) + if ($years > 0 OR $months > 0) + { + if ($months > 0) { - if ($months > 0) - { - $str[] = $months.' '.$CI->lang->line((($months > 1) ? 'date_months' : 'date_month')); - } - - $seconds -= $months * 2628000; + $str .= $months.' '.$CI->lang->line((($months > 1) ? 'date_months' : 'date_month')).', '; } + + $seconds -= $months * 2629743; } - // weeks - - if (count($str) < $units) { - $weeks = floor($seconds / 604800); + $weeks = floor($seconds / 604800); - if ($years > 0 OR $months > 0 OR $weeks > 0) + if ($years > 0 OR $months > 0 OR $weeks > 0) + { + if ($weeks > 0) { - if ($weeks > 0) - { - $str[] = $weeks.' '.$CI->lang->line((($weeks > 1) ? 'date_weeks' : 'date_week')); - } - - $seconds -= $weeks * 604800; + $str .= $weeks.' '.$CI->lang->line((($weeks > 1) ? 'date_weeks' : 'date_week')).', '; } - } - // days + $seconds -= $weeks * 604800; + } - if (count($str) < $units) { - $days = floor($seconds / 86400); + $days = floor($seconds / 86400); - if ($months > 0 OR $weeks > 0 OR $days > 0) + if ($months > 0 OR $weeks > 0 OR $days > 0) + { + if ($days > 0) { - if ($days > 0) - { - $str[] = $days.' '.$CI->lang->line((($days > 1) ? 'date_days' : 'date_day')); - } - - $seconds -= $days * 86400; + $str .= $days.' '.$CI->lang->line((($days > 1) ? 'date_days' : 'date_day')).', '; } - } - // hours - - if (count($str) < $units) { + $seconds -= $days * 86400; + } - $hours = floor($seconds / 3600); + $hours = floor($seconds / 3600); - if ($days > 0 OR $hours > 0) + if ($days > 0 OR $hours > 0) + { + if ($hours > 0) { - if ($hours > 0) - { - $str[] = $hours.' '.$CI->lang->line((($hours > 1) ? 'date_hours' : 'date_hour')); - } - - $seconds -= $hours * 3600; + $str .= $hours.' '.$CI->lang->line((($hours > 1) ? 'date_hours' : 'date_hour')).', '; } - } - - // minutes - if (count($str) < $units) { + $seconds -= $hours * 3600; + } - $minutes = floor($seconds / 60); + $minutes = floor($seconds / 60); - if ($days > 0 OR $hours > 0 OR $minutes > 0) + if ($days > 0 OR $hours > 0 OR $minutes > 0) + { + if ($minutes > 0) { - if ($minutes > 0) - { - $str[] = $minutes.' '.$CI->lang->line((($minutes > 1) ? 'date_minutes' : 'date_minute')); - } - - $seconds -= $minutes * 60; + $str .= $minutes.' '.$CI->lang->line((($minutes > 1) ? 'date_minutes' : 'date_minute')).', '; } - } - // seconds + $seconds -= $minutes * 60; + } - if (count($str) == 0) { - { - $str[] = $seconds.' '.$CI->lang->line((($seconds > 1) ? 'date_seconds' : 'date_second')); - } + if ($str == '') + { + $str .= $seconds.' '.$CI->lang->line((($seconds > 1) ? 'date_seconds' : 'date_second')).', '; } - return strtolower(implode(', ', $str)); + return substr(trim($str), 0, -1); } } -- cgit v1.2.3-24-g4f1b From 04c146d95dbc1c86e477bd27798d3234f74833e0 Mon Sep 17 00:00:00 2001 From: Roger Herbert Date: Sun, 11 Mar 2012 15:31:01 +0000 Subject: refactored to start from latest version, also less diff pain --- system/helpers/date_helper.php | 35 ++++++++++++++++++++--------------- 1 file changed, 20 insertions(+), 15 deletions(-) (limited to 'system/helpers/date_helper.php') diff --git a/system/helpers/date_helper.php b/system/helpers/date_helper.php index 2a34cf93e..70b01e348 100644 --- a/system/helpers/date_helper.php +++ b/system/helpers/date_helper.php @@ -163,7 +163,7 @@ if ( ! function_exists('standard_date')) */ if ( ! function_exists('timespan')) { - function timespan($seconds = 1, $time = '') + function timespan($seconds = 1, $time = '', $units = '') { $CI =& get_instance(); $CI->lang->load('date'); @@ -178,24 +178,29 @@ if ( ! function_exists('timespan')) $time = time(); } + if ( ! is_numeric($units)) + { + $units = 999; + } + $seconds = ($time <= $seconds) ? 1 : $time - $seconds; - $str = ''; + $str = array(); $years = floor($seconds / 31557600); if ($years > 0) { - $str .= $years.' '.$CI->lang->line((($years > 1) ? 'date_years' : 'date_year')).', '; + $str[] = $years.' '.$CI->lang->line((($years > 1) ? 'date_years' : 'date_year')); } $seconds -= $years * 31557600; $months = floor($seconds / 2629743); - if ($years > 0 OR $months > 0) + if (count($str) < $units AND ($years > 0 OR $months > 0)) { if ($months > 0) { - $str .= $months.' '.$CI->lang->line((($months > 1) ? 'date_months' : 'date_month')).', '; + $str[] = $months.' '.$CI->lang->line((($months > 1) ? 'date_months' : 'date_month')); } $seconds -= $months * 2629743; @@ -203,11 +208,11 @@ if ( ! function_exists('timespan')) $weeks = floor($seconds / 604800); - if ($years > 0 OR $months > 0 OR $weeks > 0) + if (count($str) < $units AND ($years > 0 OR $months > 0 OR $weeks > 0)) { if ($weeks > 0) { - $str .= $weeks.' '.$CI->lang->line((($weeks > 1) ? 'date_weeks' : 'date_week')).', '; + $str[] = $weeks.' '.$CI->lang->line((($weeks > 1) ? 'date_weeks' : 'date_week')); } $seconds -= $weeks * 604800; @@ -215,11 +220,11 @@ if ( ! function_exists('timespan')) $days = floor($seconds / 86400); - if ($months > 0 OR $weeks > 0 OR $days > 0) + if (count($str) < $units AND ($months > 0 OR $weeks > 0 OR $days > 0)) { if ($days > 0) { - $str .= $days.' '.$CI->lang->line((($days > 1) ? 'date_days' : 'date_day')).', '; + $str[] = $days.' '.$CI->lang->line((($days > 1) ? 'date_days' : 'date_day')); } $seconds -= $days * 86400; @@ -227,11 +232,11 @@ if ( ! function_exists('timespan')) $hours = floor($seconds / 3600); - if ($days > 0 OR $hours > 0) + if (count($str) < $units AND ($days > 0 OR $hours > 0)) { if ($hours > 0) { - $str .= $hours.' '.$CI->lang->line((($hours > 1) ? 'date_hours' : 'date_hour')).', '; + $str[] = $hours.' '.$CI->lang->line((($hours > 1) ? 'date_hours' : 'date_hour')); } $seconds -= $hours * 3600; @@ -239,11 +244,11 @@ if ( ! function_exists('timespan')) $minutes = floor($seconds / 60); - if ($days > 0 OR $hours > 0 OR $minutes > 0) + if (count($str) < $units AND ($days > 0 OR $hours > 0 OR $minutes > 0)) { if ($minutes > 0) { - $str .= $minutes.' '.$CI->lang->line((($minutes > 1) ? 'date_minutes' : 'date_minute')).', '; + $str[] = $minutes.' '.$CI->lang->line((($minutes > 1) ? 'date_minutes' : 'date_minute')); } $seconds -= $minutes * 60; @@ -251,10 +256,10 @@ if ( ! function_exists('timespan')) if ($str == '') { - $str .= $seconds.' '.$CI->lang->line((($seconds > 1) ? 'date_seconds' : 'date_second')).', '; + $str[] = $seconds.' '.$CI->lang->line((($seconds > 1) ? 'date_seconds' : 'date_second')); } - return substr(trim($str), 0, -1); + return implode(', ', $str); } } -- cgit v1.2.3-24-g4f1b From b8fb66b38e233c82f0116f9bdb0fdfd4ee074b30 Mon Sep 17 00:00:00 2001 From: Roger Herbert Date: Sun, 11 Mar 2012 16:15:15 +0000 Subject: AND -> && --- system/helpers/date_helper.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'system/helpers/date_helper.php') diff --git a/system/helpers/date_helper.php b/system/helpers/date_helper.php index 70b01e348..e792ecc43 100644 --- a/system/helpers/date_helper.php +++ b/system/helpers/date_helper.php @@ -196,7 +196,7 @@ if ( ! function_exists('timespan')) $seconds -= $years * 31557600; $months = floor($seconds / 2629743); - if (count($str) < $units AND ($years > 0 OR $months > 0)) + if (count($str) < $units && ($years > 0 OR $months > 0)) { if ($months > 0) { @@ -208,7 +208,7 @@ if ( ! function_exists('timespan')) $weeks = floor($seconds / 604800); - if (count($str) < $units AND ($years > 0 OR $months > 0 OR $weeks > 0)) + if (count($str) < $units && ($years > 0 OR $months > 0 OR $weeks > 0)) { if ($weeks > 0) { @@ -220,7 +220,7 @@ if ( ! function_exists('timespan')) $days = floor($seconds / 86400); - if (count($str) < $units AND ($months > 0 OR $weeks > 0 OR $days > 0)) + if (count($str) < $units && ($months > 0 OR $weeks > 0 OR $days > 0)) { if ($days > 0) { @@ -232,7 +232,7 @@ if ( ! function_exists('timespan')) $hours = floor($seconds / 3600); - if (count($str) < $units AND ($days > 0 OR $hours > 0)) + if (count($str) < $units && ($days > 0 OR $hours > 0)) { if ($hours > 0) { @@ -244,7 +244,7 @@ if ( ! function_exists('timespan')) $minutes = floor($seconds / 60); - if (count($str) < $units AND ($days > 0 OR $hours > 0 OR $minutes > 0)) + if (count($str) < $units && ($days > 0 OR $hours > 0 OR $minutes > 0)) { if ($minutes > 0) { -- cgit v1.2.3-24-g4f1b From b81f909f8aaa3bedc3820c0d4c9056b57113b46e Mon Sep 17 00:00:00 2001 From: Roger Herbert Date: Mon, 12 Mar 2012 12:46:02 +0000 Subject: updated docs & changelog --- system/helpers/date_helper.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'system/helpers/date_helper.php') diff --git a/system/helpers/date_helper.php b/system/helpers/date_helper.php index e792ecc43..2ad287b4a 100644 --- a/system/helpers/date_helper.php +++ b/system/helpers/date_helper.php @@ -159,7 +159,8 @@ if ( ! function_exists('standard_date')) * @access public * @param integer a number of seconds * @param integer Unix timestamp - * @return integer + * @param integer a number of display units + * @return string */ if ( ! function_exists('timespan')) { -- cgit v1.2.3-24-g4f1b From 8d69aa166ca642cbbaed561878e22470720ecb78 Mon Sep 17 00:00:00 2001 From: Roger Herbert Date: Wed, 14 Mar 2012 08:44:55 +0000 Subject: changed default value for $units to 7 --- system/helpers/date_helper.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'system/helpers/date_helper.php') diff --git a/system/helpers/date_helper.php b/system/helpers/date_helper.php index 2ad287b4a..9eea02561 100644 --- a/system/helpers/date_helper.php +++ b/system/helpers/date_helper.php @@ -164,7 +164,7 @@ if ( ! function_exists('standard_date')) */ if ( ! function_exists('timespan')) { - function timespan($seconds = 1, $time = '', $units = '') + function timespan($seconds = 1, $time = '', $units = 7) { $CI =& get_instance(); $CI->lang->load('date'); @@ -181,7 +181,7 @@ if ( ! function_exists('timespan')) if ( ! is_numeric($units)) { - $units = 999; + $units = 7; } $seconds = ($time <= $seconds) ? 1 : $time - $seconds; -- cgit v1.2.3-24-g4f1b From 597eb2178a0e51c105ea1d15717a9912450eb110 Mon Sep 17 00:00:00 2001 From: Roger Herbert Date: Wed, 14 Mar 2012 09:06:17 +0000 Subject: check array count for seconds unit too --- system/helpers/date_helper.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'system/helpers/date_helper.php') diff --git a/system/helpers/date_helper.php b/system/helpers/date_helper.php index 9eea02561..2cbfd5a92 100644 --- a/system/helpers/date_helper.php +++ b/system/helpers/date_helper.php @@ -255,7 +255,7 @@ if ( ! function_exists('timespan')) $seconds -= $minutes * 60; } - if ($str == '') + if (count($str) === 0) { $str[] = $seconds.' '.$CI->lang->line((($seconds > 1) ? 'date_seconds' : 'date_second')); } -- cgit v1.2.3-24-g4f1b