summaryrefslogtreecommitdiffstats
path: root/system/helpers/date_helper.php
diff options
context:
space:
mode:
authorIban Eguia <admin@razican.com>2012-06-08 23:01:31 +0200
committerIban Eguia <admin@razican.com>2012-06-08 23:01:31 +0200
commit895e98c0f04f1087e8900ce8423ad3210a423770 (patch)
tree6adfe487293df38807084599f1f04157b5531e54 /system/helpers/date_helper.php
parent0ed4f63f4268b0c98f549ffd711702fd45a761d0 (diff)
parenta593c69de4ea125c096f611c78dd0839489e7ebd (diff)
Merge remote-tracking branch 'upstream/develop' into new_date
Diffstat (limited to 'system/helpers/date_helper.php')
-rw-r--r--system/helpers/date_helper.php345
1 files changed, 171 insertions, 174 deletions
diff --git a/system/helpers/date_helper.php b/system/helpers/date_helper.php
index 14d973f65..3b0c3289d 100644
--- a/system/helpers/date_helper.php
+++ b/system/helpers/date_helper.php
@@ -37,17 +37,17 @@
// ------------------------------------------------------------------------
-/**
- * Get "now" time
- *
- * Returns time() based on the timezone parameter or on the "timezone"
- * setting
- *
- * @param string
- * @return int
- */
if ( ! function_exists('now'))
{
+ /**
+ * Get "now" time
+ *
+ * Returns time() based on the timezone parameter or on the "timezone"
+ * setting
+ *
+ * @param string
+ * @return int
+ */
function now($timezone = NULL)
{
$CI =& get_instance();
@@ -68,37 +68,37 @@ if ( ! function_exists('now'))
// ------------------------------------------------------------------------
-/**
- * Convert MySQL Style Datecodes
- *
- * This function is identical to PHPs date() function,
- * except that it allows date codes to be formatted using
- * the MySQL style, where each code letter is preceded
- * with a percent sign: %Y %m %d etc...
- *
- * The benefit of doing dates this way is that you don't
- * have to worry about escaping your text letters that
- * match the date codes.
- *
- * @param string
- * @param int
- * @return int
- */
if ( ! function_exists('mdate'))
{
+ /**
+ * Convert MySQL Style Datecodes
+ *
+ * This function is identical to PHPs date() function,
+ * except that it allows date codes to be formatted using
+ * the MySQL style, where each code letter is preceded
+ * with a percent sign: %Y %m %d etc...
+ *
+ * The benefit of doing dates this way is that you don't
+ * have to worry about escaping your text letters that
+ * match the date codes.
+ *
+ * @param string
+ * @param int
+ * @return int
+ */
function mdate($datestr = '', $time = '')
{
- if ($datestr == '')
+ if ($datestr === '')
{
return '';
}
- $time = ($time == '') ? now() : $time;
+ $time = ($time === '') ? now() : $time;
$datestr = str_replace(
'%\\',
'',
- preg_replace("/([a-z]+?){1}/i", "\\\\\\1", $datestr)
+ preg_replace('/([a-z]+?){1}/i', '\\\\\\1', $datestr)
);
return date($datestr, $time);
@@ -107,17 +107,17 @@ if ( ! function_exists('mdate'))
// ------------------------------------------------------------------------
-/**
- * Standard Date
- *
- * Returns a date formatted according to the submitted standard.
- *
- * @param string the chosen format
- * @param int Unix timestamp
- * @return string
- */
if ( ! function_exists('standard_date'))
{
+ /**
+ * Standard Date
+ *
+ * Returns a date formatted according to the submitted standard.
+ *
+ * @param string the chosen format
+ * @param int Unix timestamp
+ * @return string
+ */
function standard_date($fmt = 'DATE_RFC822', $time = '')
{
$formats = array(
@@ -144,19 +144,19 @@ if ( ! function_exists('standard_date'))
// ------------------------------------------------------------------------
-/**
- * Timespan
- *
- * Returns a span of seconds in this format:
- * 10 days 14 hours 36 minutes 47 seconds
- *
- * @param int a number of seconds
- * @param int Unix timestamp
- * @param int a number of display units
- * @return string
- */
if ( ! function_exists('timespan'))
{
+ /**
+ * Timespan
+ *
+ * Returns a span of seconds in this format:
+ * 10 days 14 hours 36 minutes 47 seconds
+ *
+ * @param int a number of seconds
+ * @param int Unix timestamp
+ * @param int a number of display units
+ * @return string
+ */
function timespan($seconds = 1, $time = '', $units = 7)
{
$CI =& get_instance();
@@ -259,18 +259,18 @@ if ( ! function_exists('timespan'))
// ------------------------------------------------------------------------
-/**
- * Number of days in a month
- *
- * Takes a month/year as input and returns the number of days
- * for the given month/year. Takes leap years into consideration.
- *
- * @param int a numeric month
- * @param int a numeric year
- * @return int
- */
if ( ! function_exists('days_in_month'))
{
+ /**
+ * Number of days in a month
+ *
+ * Takes a month/year as input and returns the number of days
+ * for the given month/year. Takes leap years into consideration.
+ *
+ * @param int a numeric month
+ * @param int a numeric year
+ * @return int
+ */
function days_in_month($month = 0, $year = '')
{
if ($month < 1 OR $month > 12)
@@ -278,14 +278,14 @@ if ( ! function_exists('days_in_month'))
return 0;
}
- if ( ! is_numeric($year) OR strlen($year) != 4)
+ if ( ! is_numeric($year) OR strlen($year) !== 4)
{
$year = date('Y');
}
if ($month == 2)
{
- if ($year % 400 == 0 OR ($year % 4 == 0 && $year % 100 != 0))
+ if ($year % 400 === 0 OR ($year % 4 === 0 && $year % 100 !== 0))
{
return 29;
}
@@ -298,58 +298,58 @@ if ( ! function_exists('days_in_month'))
// ------------------------------------------------------------------------
-/**
- * Converts a local Unix timestamp to GMT
- *
- * @param int Unix timestamp
- * @return int
- */
if ( ! function_exists('local_to_gmt'))
{
+ /**
+ * Converts a local Unix timestamp to GMT
+ *
+ * @param int Unix timestamp
+ * @return int
+ */
function local_to_gmt($time = '')
{
- if ($time == '')
+ if ($time === '')
{
$time = time();
}
return mktime(
- gmdate("H", $time),
- gmdate("i", $time),
- gmdate("s", $time),
- gmdate("m", $time),
- gmdate("d", $time),
- gmdate("Y", $time)
+ gmdate('H', $time),
+ gmdate('i', $time),
+ gmdate('s', $time),
+ gmdate('m', $time),
+ gmdate('d', $time),
+ gmdate('Y', $time)
);
}
}
// ------------------------------------------------------------------------
-/**
- * Converts GMT time to a localized value
- *
- * Takes a Unix timestamp (in GMT) as input, and returns
- * at the local value based on the timezone and DST setting
- * submitted
- *
- * @param int Unix timestamp
- * @param string timezone
- * @param bool whether DST is active
- * @return int
- */
if ( ! function_exists('gmt_to_local'))
{
+ /**
+ * Converts GMT time to a localized value
+ *
+ * Takes a Unix timestamp (in GMT) as input, and returns
+ * at the local value based on the timezone and DST setting
+ * submitted
+ *
+ * @param int Unix timestamp
+ * @param string timezone
+ * @param bool whether DST is active
+ * @return int
+ */
function gmt_to_local($time = '', $timezone = 'UTC', $dst = FALSE)
{
- if ($time == '')
+ if ($time === '')
{
return now();
}
$time += timezones($timezone) * 3600;
- if ($dst == TRUE)
+ if ($dst === TRUE)
{
$time += 3600;
}
@@ -360,14 +360,14 @@ if ( ! function_exists('gmt_to_local'))
// ------------------------------------------------------------------------
-/**
- * Converts a MySQL Timestamp to Unix
- *
- * @param int Unix timestamp
- * @return int
- */
if ( ! function_exists('mysql_to_unix'))
{
+ /**
+ * Converts a MySQL Timestamp to Unix
+ *
+ * @param int Unix timestamp
+ * @return int
+ */
function mysql_to_unix($time = '')
{
// We'll remove certain characters for backward compatibility
@@ -392,23 +392,23 @@ if ( ! function_exists('mysql_to_unix'))
// ------------------------------------------------------------------------
-/**
- * Unix to "Human"
- *
- * Formats Unix timestamp to the following prototype: 2006-08-21 11:35 PM
- *
- * @param int Unix timestamp
- * @param bool whether to show seconds
- * @param string format: us or euro
- * @return string
- */
if ( ! function_exists('unix_to_human'))
{
+ /**
+ * Unix to "Human"
+ *
+ * Formats Unix timestamp to the following prototype: 2006-08-21 11:35 PM
+ *
+ * @param int Unix timestamp
+ * @param bool whether to show seconds
+ * @param string format: us or euro
+ * @return string
+ */
function unix_to_human($time = '', $seconds = FALSE, $fmt = 'us')
{
$r = date('Y', $time).'-'.date('m', $time).'-'.date('d', $time).' ';
- if ($fmt == 'us')
+ if ($fmt === 'us')
{
$r .= date('h', $time).':'.date('i', $time);
}
@@ -422,7 +422,7 @@ if ( ! function_exists('unix_to_human'))
$r .= ':'.date('s', $time);
}
- if ($fmt == 'us')
+ if ($fmt === 'us')
{
$r .= ' '.date('A', $time);
}
@@ -433,25 +433,24 @@ if ( ! function_exists('unix_to_human'))
// ------------------------------------------------------------------------
-/**
- * Convert "human" date to GMT
- *
- * Reverses the above process
- *
- * @param string format: us or euro
- * @return int
- */
if ( ! function_exists('human_to_unix'))
{
+ /**
+ * Convert "human" date to GMT
+ *
+ * Reverses the above process
+ *
+ * @param string format: us or euro
+ * @return int
+ */
function human_to_unix($datestr = '')
{
- if ($datestr == '')
+ if ($datestr === '')
{
return FALSE;
}
- $datestr = trim($datestr);
- $datestr = preg_replace("/\040+/", ' ', $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))
{
@@ -460,20 +459,20 @@ if ( ! function_exists('human_to_unix'))
$split = explode(' ', $datestr);
- $ex = 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'];
+ $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 = (strlen($ex['0']) == 1) ? '0'.$ex['0'] : $ex['0'];
- $min = (strlen($ex['1']) == 1) ? '0'.$ex['1'] : $ex['1'];
+ $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']))
+ if (isset($ex[2]) && preg_match('/[0-9]{1,2}/', $ex[2]))
{
- $sec = (strlen($ex['2']) == 1) ? '0'.$ex['2'] : $ex['2'];
+ $sec = (strlen($ex[2]) === 1) ? '0'.$ex[2] : $ex[2];
}
else
{
@@ -481,13 +480,13 @@ if ( ! function_exists('human_to_unix'))
$sec = '00';
}
- if (isset($split['2']))
+ if (isset($split[2]))
{
- $ampm = strtolower($split['2']);
+ $ampm = strtolower($split[2]);
if (substr($ampm, 0, 1) === 'p' && $hour < 12)
{
- $hour = $hour + 12;
+ $hour += 12;
}
if (substr($ampm, 0, 1) === 'a' && $hour == 12)
@@ -495,7 +494,7 @@ if ( ! function_exists('human_to_unix'))
$hour = '00';
}
- if (strlen($hour) == 1)
+ if (strlen($hour) === 1)
{
$hour = '0'.$hour;
}
@@ -507,16 +506,16 @@ if ( ! function_exists('human_to_unix'))
// ------------------------------------------------------------------------
-/**
- * Turns many "reasonably-date-like" strings into something
- * that is actually useful. This only works for dates after unix epoch.
- *
- * @param string The terribly formatted date-like string
- * @param string Date format to return (same as php date function)
- * @return string
- */
if ( ! function_exists('nice_date'))
{
+ /**
+ * Turns many "reasonably-date-like" strings into something
+ * that is actually useful. This only works for dates after unix epoch.
+ *
+ * @param string The terribly formatted date-like string
+ * @param string Date format to return (same as php date function)
+ * @return string
+ */
function nice_date($bad_date = '', $format = FALSE)
{
if (empty($bad_date))
@@ -527,7 +526,7 @@ if ( ! function_exists('nice_date'))
// Date like: YYYYMM
if (preg_match('/^\d{6}$/', $bad_date))
{
- if (in_array(substr($bad_date, 0, 2),array('19', '20')))
+ if (in_array(substr($bad_date, 0, 2), array('19', '20')))
{
$year = substr($bad_date, 0, 4);
$month = substr($bad_date, 4, 2);
@@ -538,32 +537,32 @@ if ( ! function_exists('nice_date'))
$year = substr($bad_date, 2, 4);
}
- return date($format, strtotime($year . '-' . $month . '-01'));
+ return date($format, strtotime($year.'-'.$month.'-01'));
}
// Date Like: YYYYMMDD
- if (preg_match('/^\d{8}$/',$bad_date))
+ 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);
- return date($format, strtotime($month . '/01/' . $year));
+ 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))
{
list($m, $d, $y) = explode('-', $bad_date);
- return date($format, strtotime("{$y}-{$m}-{$d}"));
+ return date($format, strtotime($y.'-'.$m.'-'.$d));
}
// Any other kind of string, when converted into UNIX time,
// produces "0 seconds after epoc..." is probably bad...
// return "Invalid Date".
- if (date('U', strtotime($bad_date)) == '0')
+ if (date('U', strtotime($bad_date)) === '0')
{
- return "Invalid Date";
+ return 'Invalid Date';
}
// It's probably a valid-ish date format already
@@ -573,28 +572,28 @@ if ( ! function_exists('nice_date'))
// ------------------------------------------------------------------------
-/**
- * Timezone Menu
- *
- * Generates a drop-down menu of timezones.
- *
- * @param string timezone
- * @param string classname
- * @param string menu name
- * @return string
- */
if ( ! function_exists('timezone_menu'))
{
- function timezone_menu($default = 'UTC', $class = "", $name = 'timezones')
+ /**
+ * Timezone Menu
+ *
+ * Generates a drop-down menu of timezones.
+ *
+ * @param string timezone
+ * @param string classname
+ * @param string menu name
+ * @return string
+ */
+ function timezone_menu($default = 'UTC', $class = '', $name = 'timezones')
{
$CI =& get_instance();
$CI->lang->load('date');
- $default = ($default == 'GMT') ? 'UTC' : $default;
+ $default = ($default === 'GMT') ? 'UTC' : $default;
$menu = '<select name="'.$name.'"';
- if ($class != '')
+ if ($class !== '')
{
$menu .= ' class="'.$class.'"';
}
@@ -603,29 +602,27 @@ if ( ! function_exists('timezone_menu'))
foreach (timezones() as $key => $val)
{
- $selected = ($default == $key) ? " selected='selected'" : '';
- $menu .= "<option value='{$key}'{$selected}>".$CI->lang->line($key)."</option>\n";
+ $selected = ($default === $key) ? ' selected="selected"' : '';
+ $menu .= '<option value="'.$key.'"'.$selected.'>'.$CI->lang->line($key)."</option>\n";
}
- $menu .= "</select>";
-
- return $menu;
+ return $menu.'</select>';
}
}
// ------------------------------------------------------------------------
-/**
- * Timezones
- *
- * Returns an array of timezones. This is a helper function
- * for various other ones in this library
- *
- * @param string timezone
- * @return string
- */
if ( ! function_exists('timezones'))
{
+ /**
+ * Timezones
+ *
+ * Returns an array of timezones. This is a helper function
+ * for various other ones in this library
+ *
+ * @param string timezone
+ * @return string
+ */
function timezones($tz = '')
{
// Note: Don't change the order of these even though
@@ -674,12 +671,12 @@ if ( ! function_exists('timezones'))
'UP14' => +14
);
- if ($tz == '')
+ if ($tz === '')
{
return $zones;
}
- $tz = ($tz == 'GMT') ? 'UTC' : $tz;
+ $tz = ($tz === 'GMT') ? 'UTC' : $tz;
return isset($zones[$tz]) ? $zones[$tz] : 0;
}