<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
* CodeIgniter
*
* An open source application development framework for PHP 5.1.6 or newer
*
* @package CodeIgniter
* @author ExpressionEngine Dev Team
* @copyright Copyright (c) 2008 - 2011, EllisLab, Inc.
* @license http://codeigniter.com/user_guide/license.html
* @link http://codeigniter.com
* @since Version 1.0
* @filesource
*/
// ------------------------------------------------------------------------
/**
* CodeIgniter Date Helpers
* CI 日期帮助
* @package CodeIgniter
* @subpackage Helpers
* @category Helpers
* @author ExpressionEngine Dev Team
* @link http://codeigniter.com/user_guide/helpers/date_helper.html
*/
// ------------------------------------------------------------------------
/**
* Get "now" time
* “现在”时间
* Returns time() or its GMT equivalent based on the config file preference
* 返回时间()或GMT相当于基于配置文件偏好
* @access public
* @return integer 定义now()函数
*/
if ( ! function_exists('now'))
{
function now()
{
$CI =& get_instance();
//如果CI config中用的是teim_reference == gmt的话
if (strtolower($CI->config->item('time_reference')) == 'gmt')
{
$now = time(); //当前时间
//gmdate() 函数格式化GMT/UTC 日期/时间
//同date()函数类似,不同的是返回时间是格林威治标准时(GMT)
$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.Date类不能设置一个适当的GMT时间戳,所以本地时间()值。');
}
return $system_time;
}
else
{
return time();
}
}
}
// ------------------------------------------------------------------------
/**
* Convert MySQL Style Datecodes
* MySQL的风格转换日期代码
*
* 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...
* 此功能PHPS date()函数相同,
* 只是它允许被格式化的日期代码使用MySQL的风格,
* 每个代码字母前面百分号:%Y%M%D等..
*
* 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.
* 日期这样做的好处是,你不这样做有担心逃脱你的文字字母相匹配的日期代码。
* @access public
* @param string
* @param integer
* @return integer
*/
if ( ! function_exists('mdate'))
{
function mdate($datestr = '', $time = '')
{
if ($datestr == '')
return '';
if ($time == '')
$time = now();
// preg_replace("/([a-z]+?){1}/i", "\\\\\\1", $datestr)
// 只要是a-z 后面加上一次或多次的{1} 替换为\\\\\1
$datestr = str_replace('%\\', '', preg_replace("/([a-z]+?){1}/i", "\\\\\\1", $datestr));
return date($datestr, $time);
}
}
// ------------------------------------------------------------------------
/**
* Standard Date
* 标准日期
*
* Returns a date formatted according to the submitted standard.
* 根据提交的标准格式返回日期。
* @access public
* @param string the chosen format //格式
* @param integer Unix timestamp //时间截
* @return string
*/
if ( ! function_exists('standard_date'))
{
function standard_date($fmt = 'DATE_RFC822', $time = '')
{
$formats = array(
'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'
);
//DATE_ATOM
//DATE_COOKIE
//DATE_ISO8601
//DATE_RFC822
//DATE_REC850
//DATE_RFC1036
//DATE_RFC1123
//DATE_RSS
//DATE_W3C
//如果指定的格式不存在
if ( ! isset($formats[$fmt]))
{
return FALSE;
}
//mdate
return mdate($formats[$fmt], $time);
}
}
// ------------------------------------------------------------------------
/**
* Timespan
* 时间跨度
*
* Returns a span of seconds in this format:
* 10 days 14 hours 36 minutes 47 seconds
* *返回跨度秒格式为:10天14小时36分钟47秒 days hours minutes seconds
* @access public
* @param integer a number of seconds 秒数
* @param integer Unix timestamp 时间
* @return integer
*/
if ( ! function_exists('timespan'))
{
function timespan($seconds = 1, $time = '')
{
$CI =& get_instance();
$CI->lang->load('date'); //$CI->lang->load('date');?这是加载的那个文档,没找到?
//如果秒数不为数值,is_numeric
if ( ! is_numeric($seconds))
{
$seconds = 1;
}
//如果时间不为数值,取当前时间time()
if ( ! is_numeric($time))
{
$time = time();
}
//如果当前时间还要小于需要计算的秒数
if ($time <= $seconds)
{
$seconds = 1;
}
else
{
//取两者之间相差的秒数
$seconds = $time - $seconds;
}
$str = '';
$years = floor($seconds / 31536000); //一年的秒数 31536000
// 如果年数大于0
if ($years > 0)
{
//如果$years 大于1 取lang->line中的date_years这行,或者取date_year这行
$str .= $years.' '.$CI->lang->line((($years > 1) ? 'date_years' : 'date_year')).', ';
}
$seconds -= $years * 31536000; //用相差的秒数减去已经取得的年数
$months = floor($seconds / 2628000); //一月的秒数 2628000
//如果年大于0 或者月大于0
if ($years > 0 OR $months > 0)
{
//月大于0
if ($months > 0)
{
//
$str .= $months.' '.$CI->lang->line((($months > 1) ? 'date_months' : 'date_month')).', ';
}
//减去月所在的秒数
$seconds -= $months * 2628000;
}
//一周的秒数 604800
$weeks = floor($seconds / 604800);
//如果年大于0或者月大于0或者周大于0
if ($years > 0 OR $months > 0 OR $weeks > 0)
{
//周大于0
if ($weeks > 0)
{
$str .= $weeks.' '.$CI->lang->line((($weeks > 1) ? 'date_weeks' : 'date_week')).', ';
}
$seconds -= $weeks * 604800;
}
//一天的秒数为 86400
// floor() 返回的类型仍然是 float,因为 float 值的范围通常比 integer 要大
// floor() 返回的类型仍然是float,因为float值的范围通常比integer要大
$days = floor($seconds / 86400);
//如果月大于0 周大于0,天大于0
if ($months > 0 OR $weeks > 0 OR $days > 0)
{
if ($days > 0)
{
$str .= $days.' '.$CI->lang->line((($days > 1) ? 'date_days' : 'date_day')).', ';
}
$seconds -= $days * 86400;
}
//一小时
$hours = floor($seconds / 3600);
if ($days > 0 OR $hours > 0)
{
if ($hours > 0)
{
$str .= $hours.' '.$CI->lang->line((($hours > 1) ? 'date_hours' : 'date_hour')).', ';
}
$seconds -= $hours * 3600;
}
//一分种
$minutes = floor($seconds / 60);
if ($days > 0 OR $hours > 0 OR $minutes > 0)
{
if ($minutes > 0)
{
$str .= $minutes.' '.$CI->lang->line((($minutes > 1) ? 'date_minutes' : 'date_minute')).', ';
}
$seconds -= $minutes * 60;
}
if ($str == '')
{
$str .= $seconds.' '.$CI->lang->line((($seconds > 1) ? 'date_seconds' : 'date_second')).', ';
}
//
return substr(trim($str), 0, -1);
}
}
// ------------------------------------------------------------------------
/**
* 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.
* 需要一个月/年作为输入并返回的天数
* 给定月份/年。考虑闰年。
* @access public
* @param integer a numeric month 月
* @param integer a numeric year 年
* @return integer
*/
if ( ! function_exists('days_in_month'))
{
function days_in_month($month = 0, $year = '')
{
//如果月分小于1大于12就是不合法的
if ($month < 1 OR $month > 12)
{
return 0;
}
//如果year不为数值,或者长度不等于四位,取当前的年月
if ( ! is_numeric($year) OR strlen($year) != 4)
{
$year = date('Y');
}
//如果是二月
if ($month == 2)
{
//用年%400ak或者年%4==0 并且$year%100!=0
//不是润年
if ($year % 400 == 0 OR ($year % 4 == 0 AND $year % 100 != 0))
{
return 29;
}
}
//
$days_in_month = array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
return $days_in_month[$month - 1];
}
}
// ------------------------------------------------------------------------
/**
* Converts a local Unix timestamp to GMT
* 本地的Unix时间戳转换GMT
* @access public
* @param integer Unix timestamp
* @return integer
*/
if ( ! function_exists('local_to_gmt'))
{
function local_to_gmt($time = '')
{
if ($time == '')
$time = time();
//Hours时 mintern seconds month days year
return mktime( 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
* GMT时间转换本地化值
*
* Takes a Unix timestamp (in GMT) as input, and returns
* at the local value based on the timezone and DST setting
* submitted
* 注意到一个Unix时间戳(GMT)作为输入,并返回
* 在当地的价值基础上的时区和夏令时设置
* 提交
* @access public
* @param integer Unix timestamp
* @param string timezone
* @param bool whether DST is active 是否是活跃DST
* @return integer
*/
if ( ! function_exists('gmt_to_local'))
{
function gmt_to_local($time = '', $timezone = 'UTC', $dst = FALSE)
{
if ($time == '')
{
return now();
}
$time += timezones($timezone) * 3600;
//是否为夏令时
if ($dst == TRUE)
{
$time += 3600;
}
return $time;
}
}
// ------------------------------------------------------------------------
/**
* Converts a MySQL Timestamp to Unix
* 一个MySQL时间戳转换到Unix
* @access public
* @param integer Unix timestamp
* @return integer
*/
if ( ! function_exists('mysql_to_unix'))
{
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
// 我们会删除某些字符的向后兼容性
// 因为与MySQL4.1的格式改变
// YYYY-MM-DD HH:MM:SS
$time = str_replace('-', '', $time);
$time = str_replace(':', '', $time);
$time = str_replace(' ', '', $time);
// YYYYMMDDHHMMSS
return mktime(
substr($time, 8, 2), //hours
substr($time, 10, 2),//minute
substr($time, 12, 2),//secods
substr($time, 4, 2), //month
substr($time, 6, 2), //day
substr($time, 0, 4) //year
);
}
}
// ------------------------------------------------------------------------
/**
* Unix to "Human"
* Unix的“人”
* Formats Unix timestamp to the following prototype: 2006-08-21 11:35 PM
* Unix时间戳格式的原型如下:2006-08-2111:35 PM
* @access public
* @param integer Unix timestamp
* @param bool whether to show seconds 是否显示秒
* @param string format: us or euro 格式:我们或欧元
* @return string
*/
if ( ! function_exists('unix_to_human'))
{
function unix_to_human($time = '', $seconds = FALSE, $fmt = 'us')
{
//YYYY-MM-DD
$r = date('Y', $time).'-'.date('m', $time).'-'.date('d', $time).' ';
//如果显示时分
//YYY-MM-DD 看是选择12小时时间制,还是24小时间制显示
if ($fmt == 'us')
{
$r .= date('h', $time).':'.date('i', $time);
}
else
{
$r .= date('H', $time).':'.date('i', $time);
}
//是否显示秒数
if ($seconds)
{
$r .= ':'.date('s', $time);
}
//是否显示上午还是下秆AM PM
if ($fmt == 'us')
{
$r .= ' '.date('A', $time);
}
return $r;
}
}
// ------------------------------------------------------------------------
/**
* Convert "human" date to GMT
* 转换“人”的日期为GMT
* Reverses the above process
* 上述过程反转
* @access public
* @param string format: us or euro
* @return integer
*/
if ( ! function_exists('human_to_unix'))
{
function human_to_unix($datestr = '')
{
if ($datestr == '')
{
return FALSE;
}
$datestr = trim($datestr);//去空格
//将\040替换为空格
$datestr = preg_replace("/\040+/", ' ', $datestr);
//YYYY-MM-DD HH:II:SS [AP]M 如果不是这个格式,直接回false
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))
{
return FALSE;
}
//以空格分割
$split = explode(' ', $datestr);
//
$ex = explode("-", $split['0']); //元素1为年月日
$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']); //元素二是时分秒
$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';
}
//如果存在AM 或者PM
if (isset($split['2']))
{
//转换为小写
$ampm = strtolower($split['2']);
//如果首字符为p,并且小时大于12点,那么用12+当前小时
if (substr($ampm, 0, 1) == 'p' AND $hour < 12)
$hour = $hour + 12;
//如果为a, 并且小时等于12 那么小时为00
if (substr($ampm, 0, 1) == 'a' AND $hour == 12)
$hour = '00';
//如果小时的长度为1,那以前面拼接一个0
if (strlen($hour) == 1)
$hour = '0'.$hour;
}
return mktime($hour, $min, $sec, $month, $day, $year);
}
}
// ------------------------------------------------------------------------
/**
* Timezone Menu
* 时区菜单
*
* Generates a drop-down menu of timezones.
* 产生一个下拉菜单的时区。
*
* @access public
* @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')
{
$CI =& get_instance();
$CI->lang->load('date');
if ($default == 'GMT')
$default = 'UTC';
$menu = '<select name="'.$name.'"';
if ($class != '')
{
$menu .= ' class="'.$class.'"';
}
$menu .= ">\n";
foreach (timezones() as $key => $val)
{
$selected = ($default == $key) ? " selected='selected'" : '';
$menu .= "<option value='{$key}'{$selected}>".$CI->lang->line($key)."</option>\n";
}
$menu .= "</select>";
return $menu;
}
}
// ------------------------------------------------------------------------
/**
* Timezones
* 时区
*
* Returns an array of timezones. This is a helper function
* for various other ones in this library
* 返回一个数组的时区。这是一个辅助函数这个库中的各种其他的
* @access public
* @param string timezone
* @return string
*/
if ( ! function_exists('timezones'))
{
function timezones($tz = '')
{
// Note: Don't change the order of these even though
// some items appear to be in the wrong order
// 附注信息:请勿更改这些的顺序,即使
// 一些会出现下列选项是在了错误的的的顺序
$zones = array(
'UM12' => -12,
'UM11' => -11,
'UM10' => -10,
'UM95' => -9.5,
'UM9' => -9,
'UM8' => -8,
'UM7' => -7,
'UM6' => -6,
'UM5' => -5,
'UM45' => -4.5,
'UM4' => -4,
'UM35' => -3.5,
'UM3' => -3,
'UM2' => -2,
'UM1' => -1,
'UTC' => 0,
'UP1' => +1,
'UP2' => +2,
'UP3' => +3,
'UP35' => +3.5,
'UP4' => +4,
'UP45' => +4.5,
'UP5' => +5,
'UP55' => +5.5,
'UP575' => +5.75,
'UP6' => +6,
'UP65' => +6.5,
'UP7' => +7,
'UP8' => +8,
'UP875' => +8.75,
'UP9' => +9,
'UP95' => +9.5,
'UP10' => +10,
'UP105' => +10.5,
'UP11' => +11,
'UP115' => +11.5,
'UP12' => +12,
'UP1275' => +12.75,
'UP13' => +13,
'UP14' => +14
);
if ($tz == '')
{
return $zones;
}
if ($tz == 'GMT')
$tz = 'UTC';
return ( ! isset($zones[$tz])) ? 0 : $zones[$tz];
}
}
/* End of file date_helper.php */
/* Location: ./system/helpers/date_helper.php */