dateobject.php
来自「PHP 知识管理系统(基于树结构的知识管理系统), 英文原版的PHP源码。」· PHP 代码 · 共 1,013 行 · 第 1/3 页
PHP
1,013 行
case 'd': // day of month, 2 digits, with leading zero, 01 - 31
$output .= (($date['mday'] < 10) ? '0' . $date['mday'] : $date['mday']);
break;
case 'D': // day of week, 3 letters, Mon - Sun
$output .= date('D', 86400 * (3 + self::dayOfWeek($date['year'], $date['mon'], $date['mday'])));
break;
case 'j': // day of month, without leading zero, 1 - 31
$output .= $date['mday'];
break;
case 'l': // day of week, full string name, Sunday - Saturday
$output .= date('l', 86400 * (3 + self::dayOfWeek($date['year'], $date['mon'], $date['mday'])));
break;
case 'N': // ISO 8601 numeric day of week, 1 - 7
$day = self::dayOfWeek($date['year'], $date['mon'], $date['mday']);
if ($day == 0) {
$day = 7;
}
$output .= $day;
break;
case 'S': // english suffix for day of month, st nd rd th
if (($date['mday'] % 10) == 1) {
$output .= 'st';
} else if ((($date['mday'] % 10) == 2) and ($date['mday'] != 12)) {
$output .= 'nd';
} else if (($date['mday'] % 10) == 3) {
$output .= 'rd';
} else {
$output .= 'th';
}
break;
case 'w': // numeric day of week, 0 - 6
$output .= self::dayOfWeek($date['year'], $date['mon'], $date['mday']);
break;
case 'z': // day of year, 0 - 365
$output .= $date['yday'];
break;
// week formats
case 'W': // ISO 8601, week number of year
$output .= $this->weekNumber($date['year'], $date['mon'], $date['mday']);
break;
// month formats
case 'F': // string month name, january - december
$output .= date('F', mktime(0, 0, 0, $date['mon'], 2, 1971));
break;
case 'm': // number of month, with leading zeros, 01 - 12
$output .= (($date['mon'] < 10) ? '0' . $date['mon'] : $date['mon']);
break;
case 'M': // 3 letter month name, Jan - Dec
$output .= date('M',mktime(0, 0, 0, $date['mon'], 2, 1971));
break;
case 'n': // number of month, without leading zeros, 1 - 12
$output .= $date['mon'];
break;
case 't': // number of day in month
$output .= self::$_monthTable[$date['mon'] - 1];
break;
// year formats
case 'L': // is leap year ?
$output .= (self::isYearLeapYear($date['year'])) ? '1' : '0';
break;
case 'o': // ISO 8601 year number
$week = $this->weekNumber($date['year'], $date['mon'], $date['mday']);
if (($week > 50) and ($date['mon'] == 1)) {
$output .= ($date['year'] - 1);
} else {
$output .= $date['year'];
}
break;
case 'Y': // year number, 4 digits
$output .= $date['year'];
break;
case 'y': // year number, 2 digits
$output .= substr($date['year'], strlen($date['year']) - 2, 2);
break;
// time formats
case 'a': // lower case am/pm
$output .= (($date['hours'] >= 12) ? 'pm' : 'am');
break;
case 'A': // upper case am/pm
$output .= (($date['hours'] >= 12) ? 'PM' : 'AM');
break;
case 'B': // swatch internet time
$dayseconds = ($date['hours'] * 3600) + ($date['minutes'] * 60) + $date['seconds'];
if ($gmt === true) {
$dayseconds += 3600;
}
$output .= (int) (($dayseconds % 86400) / 86.4);
break;
case 'g': // hours without leading zeros, 12h format
if ($date['hours'] > 12) {
$hour = $date['hours'] - 12;
} else {
if ($date['hours'] == 0) {
$hour = '12';
} else {
$hour = $date['hours'];
}
}
$output .= $hour;
break;
case 'G': // hours without leading zeros, 24h format
$output .= $date['hours'];
break;
case 'h': // hours with leading zeros, 12h format
if ($date['hours'] > 12) {
$hour = $date['hours'] - 12;
} else {
if ($date['hours'] == 0) {
$hour = '12';
} else {
$hour = $date['hours'];
}
}
$output .= (($hour < 10) ? '0'.$hour : $hour);
break;
case 'H': // hours with leading zeros, 24h format
$output .= (($date['hours'] < 10) ? '0' . $date['hours'] : $date['hours']);
break;
case 'i': // minutes with leading zeros
$output .= (($date['minutes'] < 10) ? '0' . $date['minutes'] : $date['minutes']);
break;
case 's': // seconds with leading zeros
$output .= (($date['seconds'] < 10) ? '0' . $date['seconds'] : $date['seconds']);
break;
// timezone formats
case 'e': // timezone identifier
if ($gmt === true) {
$output .= gmdate('e', mktime($date['hours'], $date['minutes'], $date['seconds'],
$date['mon'], $date['mday'], 2000));
} else {
$output .= date('e', mktime($date['hours'], $date['minutes'], $date['seconds'],
$date['mon'], $date['mday'], 2000));
}
break;
case 'I': // daylight saving time or not
if ($gmt === true) {
$output .= gmdate('I', mktime($date['hours'], $date['minutes'], $date['seconds'],
$date['mon'], $date['mday'], 2000));
} else {
$output .= date('I', mktime($date['hours'], $date['minutes'], $date['seconds'],
$date['mon'], $date['mday'], 2000));
}
break;
case 'O': // difference to GMT in hours
$gmtstr = ($gmt === true) ? 0 : $this->_offset;
$output .= sprintf('%s%04d', ($gmtstr <= 0) ? '+' : '-', abs($gmtstr) / 36);
break;
case 'P': // difference to GMT with colon
$gmtstr = ($gmt === true) ? 0 : $this->_offset;
$gmtstr = sprintf('%s%04d', ($gmtstr <= 0) ? '+' : '-', abs($gmtstr) / 36);
$output = $output . substr($gmtstr, 0, 3) . ':' . substr($gmtstr, 3);
break;
case 'T': // timezone settings
if ($gmt === true) {
$output .= gmdate('T', mktime($date['hours'], $date['minutes'], $date['seconds'],
$date['mon'], $date['mday'], 2000));
} else {
$output .= date('T', mktime($date['hours'], $date['minutes'], $date['seconds'],
$date['mon'], $date['mday'], 2000));
}
break;
case 'Z': // timezone offset in seconds
$output .= ($gmt === true) ? 0 : -$this->_offset;
break;
// complete time formats
case 'c': // ISO 8601 date format
$difference = $this->_offset;
$difference = sprintf('%s%04d', ($difference <= 0) ? '+' : '-', abs($difference) / 36);
$output .= $date['year'] . '-' . $date['mon'] . '-'
. (($date['mday'] < 10) ? '0' . $date['mday'] : $date['mday']) . 'T'
. (($date['hours'] < 10) ? '0' . $date['hours'] : $date['hours'])
. ':' . (($date['minutes'] < 10) ? '0' . $date['minutes'] : $date['minutes'])
. ':' . (($date['seconds'] < 10) ? '0' . $date['seconds'] : $date['seconds'])
. $difference;
break;
case 'r': // RFC 2822 date format
$difference = $this->_offset;
$difference = sprintf('%s%04d', ($difference <= 0) ? '+' : '-', abs($difference) / 36);
$output .= gmdate('D', 86400 * (3 + self::dayOfWeek($date['year'], $date['mon'], $date['mday']))) .
', ' . (($date['mday'] < 10) ? '0' . $date['mday'] : $date['mday']) .
' ' . date('M',mktime(0, 0, 0, $date['mon'], 2, 1971)) .
' ' . $date['year'] .
' ' . (($date['hours'] < 10) ? '0'.$date['hours'] : $date['hours']) .
':' . (($date['minutes'] < 10) ? '0'.$date['minutes'] : $date['minutes']) .
':' . (($date['seconds'] < 10) ? '0'.$date['seconds'] : $date['seconds']) .
' ' . $difference;
break;
case 'U': // Unix timestamp
$output .= $timestamp;
break;
// special formats
case "\\": // next letter to print with no format
$i++;
if ($i < $length) {
$output .= $format[$i];
}
break;
default: // letter is no format so add it direct
$output .= $format[$i];
break;
}
}
return $output;
}
/**
* Returns the day of week for a Gregorian calendar date.
* 0 = sunday, 6 = saturday
*
* @param integer $year
* @param integer $month
* @param integer $day
* @return integer dayOfWeek
*/
protected static function dayOfWeek($year, $month, $day)
{
if ((1901 < $year) and ($year < 2038)) {
return (int) date('w', mktime(0, 0, 0, $month, $day, $year));
}
// gregorian correction
$correction = 0;
if (($year < 1582) or (($year == 1582) and (($month < 10) or (($month == 10) && ($day < 15))))) {
$correction = 3;
}
if ($month > 2) {
$month -= 2;
} else {
$month += 10;
$year--;
}
$day = floor((13 * $month - 1) / 5) + $day + ($year % 100) + floor(($year % 100) / 4);
$day += floor(($year / 100) / 4) - 2 * floor($year / 100) + 77 + $correction;
return (int) ($day - 7 * floor($day / 7));
}
/**
* Internal getDateParts function for handling 64bit timestamps, similar to:
* http://www.php.net/getdate
*
* Returns an array of date parts for $timestamp, relative to 1970/01/01 00:00:00 GMT/UTC.
*
* $fast specifies ALL date parts should be returned (slower)
* Default is false, and excludes $dayofweek, weekday, month and timestamp from parts returned.
*
* @param mixed $timestamp
* @param boolean $fast OPTIONAL defaults to fast (false), resulting in fewer date parts
* @return array
*/
protected function getDateParts($timestamp = null, $fast = null)
{
// actual timestamp
if ($timestamp === null) {
return getdate();
}
// 32bit timestamp
if (abs($timestamp) <= 0x7FFFFFFF) {
return @getdate($timestamp);
}
$otimestamp = $timestamp;
$numday = 0;
$month = 0;
// gregorian correction
if ($timestamp < -12219321600) {
$timestamp -= 864000;
}
// timestamp lower 0
if ($timestamp < 0) {
$sec = 0;
$act = 1970;
// iterate through 10 years table, increasing speed
foreach(self::$_yearTable as $year => $seconds) {
if ($timestamp >= $seconds) {
$i = $act;
break;
}
$sec = $seconds;
$act = $year;
}
$timestamp -= $sec;
if (!isset($i)) {
$i = $act;
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?