Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Improve the date helper #870

Closed
wants to merge 16 commits into from

2 participants

@narfbg
Owner

Just some minor speed optimizations and removed/fixed spaces.

system/helpers/date_helper.php
((6 lines not shown))
{
$CI =& get_instance();
$CI->lang->load('date');
- $default = ($default == 'GMT') ? 'UTC' : $default;

For this I normally go with:

$default === 'GMT' OR $default = 'UTC';

Ternary is for one condition and two possible answers (three parts), this is essentially the same but for one condition and a default answer (two parts).

What do people think?

@narfbg Owner
narfbg added a note

Um, that's why I've replaced the ternary with an if() - it's the same thing. I would indeed like to write 1 line of code instead of 4, but didn't want to start the readability discussion again.

@narfbg Owner
narfbg added a note

Well, $var === 'value' OR $var = 'othervalue'; is basically an if(), only without the 'if' word itself and all the brackets.

I vote for that to be a standard in such cases - yes. :)

@narfbg Owner
narfbg added a note

Used the proposed <comparison> OR <assignment> format in the last commit.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
system/helpers/date_helper.php
@@ -688,12 +653,14 @@ function timezones($tz = '')
{
return $zones;
}
-
- $tz = ($tz == 'GMT') ? 'UTC' : $tz;
+ elseif ($tz === 'GMT')

See above.

@narfbg Owner
narfbg added a note

This actually turned out to be unnecessary, as if $zones[$tz] is not set (and there's no element with the 'GMT' key in it) - 0 will be returned anyway, so deleted that line.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@narfbg narfbg closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jan 6, 2012
  1. @narfbg
Commits on Jan 10, 2012
  1. @narfbg

    Some more stuff ...

    narfbg authored
Commits on Jan 11, 2012
  1. @narfbg
Commits on Jan 12, 2012
  1. @narfbg
Commits on Jan 19, 2012
  1. @narfbg
Commits on Jan 20, 2012
  1. @narfbg

    Replace AND with &&

    narfbg authored
Commits on Jan 24, 2012
  1. @narfbg
Commits on Jan 25, 2012
  1. @narfbg

    Merge upstream branch

    narfbg authored
Commits on Feb 29, 2012
  1. @narfbg

    Merge upstream branch

    narfbg authored
Commits on Mar 13, 2012
  1. @narfbg
  2. @narfbg

    Some comments altered

    narfbg authored
Commits on Mar 14, 2012
  1. @narfbg

    Merge upstream branch

    narfbg authored
Commits on Mar 20, 2012
  1. @narfbg
  2. @narfbg

    Remove EOF newline

    narfbg authored
Commits on Mar 26, 2012
  1. @narfbg

    Merge upstream branch

    narfbg authored
Commits on May 17, 2012
  1. @narfbg

    Merge upstream branch

    narfbg authored
This page is out of date. Refresh to see the latest.
Showing with 65 additions and 119 deletions.
  1. +65 −119 system/helpers/date_helper.php
View
184 system/helpers/date_helper.php
@@ -49,22 +49,9 @@
function now()
{
$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 (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.');
- }
-
- return $system_time;
- }
-
- return time();
+ return (strtolower($CI->config->item('time_reference')) === 'gmt')
+ ? mktime(gmdate('G'), gmdate('i'), gmdate('s'), gmdate('n'), gmdate('j'), gmdate('Y'))
+ : time();
}
}
@@ -94,8 +81,10 @@ function mdate($datestr = '', $time = '')
{
return '';
}
-
- $time = ($time == '') ? now() : $time;
+ elseif ($time == '')
+ {
+ $time = now();
+ }
$datestr = str_replace(
'%\\',
@@ -123,24 +112,18 @@ function mdate($datestr = '', $time = '')
function standard_date($fmt = 'DATE_RFC822', $time = '')
{
$formats = array(
- '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%O',
- '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' => '%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%O'
- );
-
- if ( ! isset($formats[$fmt]))
- {
- return FALSE;
- }
-
- return mdate($formats[$fmt], $time);
+ 'DATE_ATOM' => '%Y-%m-%dT%H:%i:%s%Q',
+ 'DATE_COOKIE' => '%l, %d-%M-%y %H:%i:%s UTC',
+ 'DATE_ISO8601' => '%Y-%m-%dT%H:%i:%s%Q',
+ '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_RSS' => '%D, %d %M %Y %H:%i:%s %O',
+ 'DATE_W3C' => '%Y-%m-%dT%H:%i:%s%Q'
+ );
+
+ return isset($formats[$fmt]) ? mdate($formats[$fmt], $time) : FALSE;
}
}
@@ -164,12 +147,12 @@ function timespan($seconds = 1, $time = '', $units = 7)
$CI =& get_instance();
$CI->lang->load('date');
- if ( ! is_numeric($seconds))
+ if ( ! $seconds OR ! preg_match('/^[0-9]+$/', $seconds))
{
$seconds = 1;
}
- if ( ! is_numeric($time))
+ if ( ! preg_match('/^[0-9]+$/', $time))
{
$time = time();
}
@@ -179,14 +162,13 @@ function timespan($seconds = 1, $time = '', $units = 7)
$units = 7;
}
- $seconds = ($time <= $seconds) ? 1 : $time - $seconds;
-
$str = array();
+ $seconds = ($time <= $seconds) ? 1 : $time - $seconds;
$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;
@@ -196,7 +178,7 @@ function timespan($seconds = 1, $time = '', $units = 7)
{
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;
@@ -208,7 +190,7 @@ function timespan($seconds = 1, $time = '', $units = 7)
{
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;
@@ -220,7 +202,7 @@ function timespan($seconds = 1, $time = '', $units = 7)
{
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;
@@ -232,7 +214,7 @@ function timespan($seconds = 1, $time = '', $units = 7)
{
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;
@@ -244,7 +226,7 @@ function timespan($seconds = 1, $time = '', $units = 7)
{
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;
@@ -252,7 +234,7 @@ function timespan($seconds = 1, $time = '', $units = 7)
if (count($str) === 0)
{
- $str[] = $seconds.' '.$CI->lang->line((($seconds > 1) ? 'date_seconds' : 'date_second'));
+ $str[] = $seconds.' '.$CI->lang->line(($seconds > 1 ? 'date_seconds' : 'date_second'));
}
return implode(', ', $str);
@@ -280,20 +262,21 @@ function days_in_month($month = 0, $year = '')
return 0;
}
- if ( ! is_numeric($year) OR strlen($year) != 4)
+ if ( ! $year OR ! preg_match('/^[1-9]{1}[0-9]{3}$/', $year))
+ {
+ return (int) date('t', mktime(12, 0, 0, $month, 1, date('Y')));
+ }
+ elseif ($year >= 1970)
{
- $year = date('Y');
+ return (int) date('t', mktime(12, 0, 0, $month, 1, $year));
}
- if ($month == 2)
+ if ($month == 2 && ($year % 400 == 0 OR ($year % 4 == 0 && $year % 100 != 0)))
{
- if ($year % 400 == 0 OR ($year % 4 == 0 && $year % 100 != 0))
- {
- return 29;
- }
+ return 29;
}
- $days_in_month = array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
+ $days_in_month = array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
return $days_in_month[$month - 1];
}
}
@@ -316,11 +299,11 @@ function local_to_gmt($time = '')
}
return mktime(
- gmdate('H', $time),
+ gmdate('G', $time),
gmdate('i', $time),
gmdate('s', $time),
- gmdate('m', $time),
- gmdate('d', $time),
+ gmdate('n', $time),
+ gmdate('j', $time),
gmdate('Y', $time)
);
}
@@ -350,10 +333,9 @@ function gmt_to_local($time = '', $timezone = 'UTC', $dst = FALSE)
}
$time += timezones($timezone) * 3600;
-
if ($dst == TRUE)
{
- $time += 3600;
+ return $time + 3600;
}
return $time;
@@ -375,10 +357,7 @@ function mysql_to_unix($time = '')
// We'll remove certain characters for backward compatibility
// since the formatting changed with MySQL 4.1
// YYYY-MM-DD HH:MM:SS
-
- $time = str_replace('-', '', $time);
- $time = str_replace(':', '', $time);
- $time = str_replace(' ', '', $time);
+ $time = str_replace(array('-', ':', ' '), '', $time);
// YYYYMMDDHHMMSS
return mktime(
@@ -408,25 +387,17 @@ function mysql_to_unix($time = '')
*/
function unix_to_human($time = '', $seconds = FALSE, $fmt = 'us')
{
- $r = date('Y', $time).'-'.date('m', $time).'-'.date('d', $time).' ';
-
- if ($fmt == 'us')
- {
- $r .= date('h', $time).':'.date('i', $time);
- }
- else
- {
- $r .= date('H', $time).':'.date('i', $time);
- }
+ $r = date('Y', $time).'-'.date('m', $time).'-'.date('d', $time).' '
+ . date(($fmt === 'us' ? 'h' : 'H'), $time).':'.date('i', $time);
if ($seconds)
{
$r .= ':'.date('s', $time);
}
- if ($fmt == 'us')
+ if ($fmt === 'us')
{
- $r .= ' '.date('A', $time);
+ return $r.' '.date('A', $time);
}
return $r;
@@ -454,51 +425,29 @@ function human_to_unix($datestr = '')
$datestr = preg_replace('/\040+/', ' ', trim($datestr));
- if ( ! preg_match('/^[0-9]{2,4}\-[0-9]{1,2}\-[0-9]{1,2}\s[0-9]{1,2}:[0-9]{1,2}(?::[0-9]{1,2})?(?:\s[AP]M)?$/i', $datestr))
+ if ( ! preg_match('/^(\d{2}|\d{4})\-[0-9]{1,2}\-[0-9]{1,2}\s[0-9]{1,2}:[0-9]{1,2}(?::[0-9]{1,2})?(?:\s[AP]M)?$/i', $datestr))
{
return FALSE;
}
$split = explode(' ', $datestr);
+ list($year, $month, $day) = explode('-', $split[0]);
- $ex = explode('-', $split['0']);
-
- $year = (strlen($ex[0]) === 2) ? '20'.$ex[0] : $ex[0];
- $month = (strlen($ex[1]) === 1) ? '0'.$ex[1] : $ex[1];
- $day = (strlen($ex[2]) === 1) ? '0'.$ex[2] : $ex[2];
-
- $ex = explode(':', $split['1']);
+ $ex = explode(':', $split[1]);
+ $hour = (int) $ex[0];
+ $min = (int) $ex[1];
+ $sec = (isset($ex[2]) && preg_match('/[0-9]{1,2}/', $ex[2])) ? (int) $ex[2] : 0;
- $hour = (strlen($ex[0]) === 1) ? '0'.$ex[0] : $ex[0];
- $min = (strlen($ex[1]) === 1) ? '0'.$ex[1] : $ex[1];
-
- if (isset($ex['2']) && preg_match('/[0-9]{1,2}/', $ex[2]))
- {
- $sec = (strlen($ex[2]) === 1) ? '0'.$ex[2] : $ex[2];
- }
- else
- {
- // Unless specified, seconds get set to zero.
- $sec = '00';
- }
-
- if (isset($split['2']))
+ if (isset($split[2]))
{
$ampm = strtolower($split[2]);
-
- if (substr($ampm, 0, 1) === 'p' && $hour < 12)
+ if ($ampm[0] === 'p' && $hour < 12)
{
$hour += 12;
}
-
- if (substr($ampm, 0, 1) === 'a' && $hour == 12)
+ elseif ($ampm[0] === 'a' && $hour == 12)
{
- $hour = '00';
- }
-
- if (strlen($hour) === 1)
- {
- $hour = '0'.$hour;
+ $hour = 0;
}
}
@@ -545,18 +494,17 @@ function nice_date($bad_date = '', $format = FALSE)
// Date Like: YYYYMMDD
if (preg_match('/^\d{8}$/', $bad_date))
{
- $month = substr($bad_date, 0, 2);
- $day = substr($bad_date, 2, 2);
- $year = substr($bad_date, 4, 4);
+ $month = substr($bad_date, 0, 2);
+ $day = substr($bad_date, 2, 2);
+ $year = substr($bad_date, 4, 4);
return date($format, strtotime($month.'/01/'.$year));
}
// Date Like: MM-DD-YYYY __or__ M-D-YYYY (or anything in between)
- if (preg_match('/^\d{1,2}-\d{1,2}-\d{4}$/', $bad_date))
+ if (preg_match('/^(\d{1,2})\-|\.(\d{1,2})\-|\.(\d{4})$/', $bad_date, $matches))
{
- list($m, $d, $y) = explode('-', $bad_date);
- return date($format, strtotime($y.'-'.$m.'-'.$d));
+ return date($format, strtotime($matches[3].'-'.$matches[2].'-'.$matches[1]));
}
// Any other kind of string, when converted into UNIX time,
@@ -591,7 +539,7 @@ function timezone_menu($default = 'UTC', $class = '', $name = 'timezones')
$CI =& get_instance();
$CI->lang->load('date');
- $default = ($default == 'GMT') ? 'UTC' : $default;
+ $default !== 'GMT' OR $default = 'UTC';
$menu = '<select name="'.$name.'"';
@@ -605,10 +553,10 @@ function timezone_menu($default = 'UTC', $class = '', $name = 'timezones')
foreach (timezones() as $key => $val)
{
$selected = ($default == $key) ? ' selected="selected"' : '';
- $menu .= '<option value="'.$key.'"'.$selected.'>'.$CI->lang->line($key)."</option>\n";
+ $menu .= '<option value="'.$key.'"'.($default == $key ? ' selected="selected"' : '').'>'.$CI->lang->line($key)."</option>\n";
}
- return $menu.'</select>';
+ return $menu."</select>\n";
}
}
@@ -678,8 +626,6 @@ function timezones($tz = '')
return $zones;
}
- $tz = ($tz == 'GMT') ? 'UTC' : $tz;
-
return isset($zones[$tz]) ? $zones[$tz] : 0;
}
}
Something went wrong with that request. Please try again.