Permalink
Browse files

Changed now() function to return time() based on the $timezone parame…

…ter, or in the config setting, if that parameter is not set.
  • Loading branch information...
1 parent 10c0117 commit aba9d420153aad2493e65b4da8a9727c70cecc43 @Razican Razican committed Aug 23, 2011
Showing with 27 additions and 32 deletions.
  1. +27 −32 system/helpers/date_helper.php
@@ -30,34 +30,29 @@
/**
* Get "now" time
*
- * Returns time() or its GMT equivalent based on the config file preference
+ * Returns time() based on the $timezone parameter. If it's not set,
+ * it will return time() based on the master timezone. All PHP supported
+ * timezones are supported.
*
* @access public
+ * @param string
* @return integer
*/
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;
- }
- else
- {
- return time();
- }
+ return $time;
}
}
@@ -493,62 +488,62 @@ function human_to_unix($datestr = '')
/**
* Turns many "reasonably-date-like" strings into something
* that is actually useful. This only works for dates after unix epoch.
- *
+ *
* @access public
* @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'))
{
- function nice_date($bad_date='', $format=false)
+ function nice_date($bad_date='', $format=false)
{
if (empty($bad_date))
{
return 'Unknown';
}
// Date like: YYYYMM
- if (preg_match('/^\d{6}$/',$bad_date))
+ if (preg_match('/^\d{6}$/',$bad_date))
{
//echo $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);
- }
- else
+ }
+ else
{
$month = substr($bad_date, 0, 2);
$year = substr($bad_date, 2, 4);
}
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));
}
-
+
// 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))
- {
+ {
list($m, $d, $y) = explode('-', $bad_date);
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')
- {
+ {
return "Invalid Date";
}
-
+
// It's probably a valid-ish date format already
return date($format, strtotime($bad_date));
}

0 comments on commit aba9d42

Please sign in to comment.