📄 date.php
字号:
<?php
/* vim: set expandtab tabstop=4 shiftwidth=4: */
// +----------------------------------------------------------------------+
// | PHP Version 4 |
// +----------------------------------------------------------------------+
// | Copyright (c) 1997-2003 The PHP Group |
// +----------------------------------------------------------------------+
// | This source file is subject to version 2.02 of the PHP license, |
// | that is bundled with this package in the file LICENSE, and is |
// | available at through the world-wide-web at |
// | http://www.php.net/license/2_02.txt. |
// | If you did not receive a copy of the PHP license and are unable to |
// | obtain it through the world-wide-web, please send a note to |
// | license@php.net so we can mail you a copy immediately. |
// +----------------------------------------------------------------------+
// | Authors: Baba Buehler <baba@babaz.com> |
// | |
// +----------------------------------------------------------------------+
//
// $Id: Date.php,v 1.27 2004/03/14 15:22:46 pajoye Exp $
/**@#+
* Include supporting classes
*/
require_once PEAR_DIR . 'Date/TimeZone.php';
require_once PEAR_DIR . 'Date/Calc.php';
require_once PEAR_DIR . 'Date/Span.php';
/**@#-*/
/**@#+
* Output formats. Pass this to getDate().
*/
/**
* "YYYY-MM-DD HH:MM:SS"
*/
define('DATE_FORMAT_ISO', 1);
/**
* "YYYYMMSSTHHMMSS(Z|(+/-)HHMM)?"
*/
define('DATE_FORMAT_ISO_BASIC', 2);
/**
* "YYYY-MM-SSTHH:MM:SS(Z|(+/-)HH:MM)?"
*/
define('DATE_FORMAT_ISO_EXTENDED', 3);
/**
* "YYYYMMDDHHMMSS"
*/
define('DATE_FORMAT_TIMESTAMP', 4);
/**
* long int, seconds since the unix epoch
*/
define('DATE_FORMAT_UNIXTIME', 5);
/**@#-*/
/**
* Generic date handling class for PEAR.
*
* Generic date handling class for PEAR. Attempts to be time zone aware
* through the Date::TimeZone class. Supports several operations from
* Date::Calc on Date objects.
*
* @author Baba Buehler <baba@babaz.com>
* @package Date
* @access public
*/
class Date
{
/**
* the year
* @var int
*/
var $year;
/**
* the month
* @var int
*/
var $month;
/**
* the day
* @var int
*/
var $day;
/**
* the hour
* @var int
*/
var $hour;
/**
* the minute
* @var int
*/
var $minute;
/**
* the second
* @var int
*/
var $second;
/**
* timezone for this date
* @var object Date_TimeZone
*/
var $tz;
/**
* Constructor
*
* Creates a new Date Object initialized to the current date/time in the
* system-default timezone by default. A date optionally
* passed in may be in the ISO 8601, TIMESTAMP or UNIXTIME format,
* or another Date object. If no date is passed, the current date/time
* is used.
*
* @access public
* @see setDate()
* @param mixed $date optional - date/time to initialize
* @return object Date the new Date object
*/
function Date($date = null)
{
$this->tz = Date_TimeZone::getDefault();
if (is_null($date)) {
$this->setDate(date("Y-m-d H:i:s"));
} elseif (is_a($date, 'Date')) {
$this->copy($date);
} else {
$this->setDate($date);
}
}
/**
* Set the fields of a Date object based on the input date and format
*
* Set the fields of a Date object based on the input date and format,
* which is specified by the DATE_FORMAT_* constants.
*
* @access public
* @param string $date input date
* @param int $format Optional format constant (DATE_FORMAT_*) of the input date.
* This parameter isn't really needed anymore, but you could
* use it to force DATE_FORMAT_UNIXTIME.
*/
function setDate($date, $format = DATE_FORMAT_ISO)
{
if (
preg_match('/^(\d{4})-?(\d{2})-?(\d{2})([T\s]?(\d{2}):?(\d{2}):?(\d{2})(Z|[\+\-]\d{2}:?\d{2})?)?$/i', $date, $regs)
&& $format != DATE_FORMAT_UNIXTIME) {
// DATE_FORMAT_ISO, ISO_BASIC, ISO_EXTENDED, and TIMESTAMP
// These formats are extremely close to each other. This regex
// is very loose and accepts almost any butchered format you could
// throw at it. e.g. 2003-10-07 19:45:15 and 2003-10071945:15
// are the same thing in the eyes of this regex, even though the
// latter is not a valid ISO 8601 date.
$this->year = $regs[1];
$this->month = $regs[2];
$this->day = $regs[3];
$this->hour = isset($regs[5])?$regs[5]:0;
$this->minute = isset($regs[6])?$regs[6]:0;
$this->second = isset($regs[7])?$regs[7]:0;
// if an offset is defined, convert time to UTC
// Date currently can't set a timezone only by offset,
// so it has to store it as UTC
if (isset($regs[8])) {
$this->toUTCbyOffset($regs[8]);
}
} elseif (is_numeric($date)) {
// UNIXTIME
$this->setDate(date("Y-m-d H:i:s", $date));
} else {
// unknown format
$this->year = 0;
$this->month = 1;
$this->day = 1;
$this->hour = 0;
$this->minute = 0;
$this->second = 0;
}
}
/**
* Get a string (or other) representation of this date
*
* Get a string (or other) representation of this date in the
* format specified by the DATE_FORMAT_* constants.
*
* @access public
* @param int $format format constant (DATE_FORMAT_*) of the output date
* @return string the date in the requested format
*/
function getDate($format = DATE_FORMAT_ISO)
{
switch ($format) {
case DATE_FORMAT_ISO:
return $this->format("%Y-%m-%d %T");
break;
case DATE_FORMAT_ISO_BASIC:
$format = "%Y%m%dT%H%M%S";
if ($this->tz->getID() == 'UTC') {
$format .= "Z";
}
return $this->format($format);
break;
case DATE_FORMAT_ISO_EXTENDED:
$format = "%Y-%m-%dT%H:%M:%S";
if ($this->tz->getID() == 'UTC') {
$format .= "Z";
}
return $this->format($format);
break;
case DATE_FORMAT_TIMESTAMP:
return $this->format("%Y%m%d%H%M%S");
break;
case DATE_FORMAT_UNIXTIME:
return mktime($this->hour, $this->minute, $this->second, $this->month, $this->day, $this->year);
break;
}
}
/**
* Copy values from another Date object
*
* Makes this Date a copy of another Date object.
*
* @access public
* @param object Date $date Date to copy from
*/
function copy($date)
{
$this->year = $date->year;
$this->month = $date->month;
$this->day = $date->day;
$this->hour = $date->hour;
$this->minute = $date->minute;
$this->second = $date->second;
$this->tz = $date->tz;
}
/**
* Date pretty printing, similar to strftime()
*
* Formats the date in the given format, much like
* strftime(). Most strftime() options are supported.<br><br>
*
* formatting options:<br><br>
*
* <code>%a </code> abbreviated weekday name (Sun, Mon, Tue) <br>
* <code>%A </code> full weekday name (Sunday, Monday, Tuesday) <br>
* <code>%b </code> abbreviated month name (Jan, Feb, Mar) <br>
* <code>%B </code> full month name (January, February, March) <br>
* <code>%C </code> century number (the year divided by 100 and truncated to an integer, range 00 to 99) <br>
* <code>%d </code> day of month (range 00 to 31) <br>
* <code>%D </code> same as "%m/%d/%y" <br>
* <code>%e </code> day of month, single digit (range 0 to 31) <br>
* <code>%E </code> number of days since unspecified epoch (integer, Date_Calc::dateToDays()) <br>
* <code>%H </code> hour as decimal number (00 to 23) <br>
* <code>%I </code> hour as decimal number on 12-hour clock (01 to 12) <br>
* <code>%j </code> day of year (range 001 to 366) <br>
* <code>%m </code> month as decimal number (range 01 to 12) <br>
* <code>%M </code> minute as a decimal number (00 to 59) <br>
* <code>%n </code> newline character (\n) <br>
* <code>%O </code> dst-corrected timezone offset expressed as "+/-HH:MM" <br>
* <code>%o </code> raw timezone offset expressed as "+/-HH:MM" <br>
* <code>%p </code> either 'am' or 'pm' depending on the time <br>
* <code>%P </code> either 'AM' or 'PM' depending on the time <br>
* <code>%r </code> time in am/pm notation, same as "%I:%M:%S %p" <br>
* <code>%R </code> time in 24-hour notation, same as "%H:%M" <br>
* <code>%S </code> seconds as a decimal number (00 to 59) <br>
* <code>%t </code> tab character (\t) <br>
* <code>%T </code> current time, same as "%H:%M:%S" <br>
* <code>%w </code> weekday as decimal (0 = Sunday) <br>
* <code>%U </code> week number of current year, first sunday as first week <br>
* <code>%y </code> year as decimal (range 00 to 99) <br>
* <code>%Y </code> year as decimal including century (range 0000 to 9999) <br>
* <code>%% </code> literal '%' <br>
* <br>
*
* @access public
* @param string format the format string for returned date/time
* @return string date/time in given format
*/
function format($format)
{
$output = "";
for($strpos = 0; $strpos < strlen($format); $strpos++) {
$char = substr($format,$strpos,1);
if ($char == "%") {
$nextchar = substr($format,$strpos + 1,1);
switch ($nextchar) {
case "a":
$output .= Date_Calc::getWeekdayAbbrname($this->day,$this->month,$this->year);
break;
case "A":
$output .= Date_Calc::getWeekdayFullname($this->day,$this->month,$this->year);
break;
case "b":
$output .= Date_Calc::getMonthAbbrname($this->month);
break;
case "B":
$output .= Date_Calc::getMonthFullname($this->month);
break;
case "C":
$output .= sprintf("%02d",intval($this->year/100));
break;
case "d":
$output .= sprintf("%02d",$this->day);
break;
case "D":
$output .= sprintf("%02d/%02d/%02d",$this->month,$this->day,$this->year);
break;
case "e":
$output .= $this->day * 1; // get rid of leading zero
break;
case "E":
$output .= Date_Calc::dateToDays($this->day,$this->month,$this->year);
break;
case "H":
$output .= sprintf("%02d", $this->hour);
break;
case "I":
$hour = ($this->hour + 1) > 12 ? $this->hour - 12 : $this->hour;
$output .= sprintf("%02d", $hour==0 ? 12 : $hour);
break;
case "j":
$output .= Date_Calc::julianDate($this->day,$this->month,$this->year);
break;
case "m":
$output .= sprintf("%02d",$this->month);
break;
case "M":
$output .= sprintf("%02d",$this->minute);
break;
case "n":
$output .= "\n";
break;
case "O":
$offms = $this->tz->getOffset($this);
$direction = $offms >= 0 ? "+" : "-";
$offmins = abs($offms) / 1000 / 60;
$hours = $offmins / 60;
$minutes = $offmins % 60;
$output .= sprintf("%s%02d:%02d", $direction, $hours, $minutes);
break;
case "o":
$offms = $this->tz->getRawOffset($this);
$direction = $offms >= 0 ? "+" : "-";
$offmins = abs($offms) / 1000 / 60;
$hours = $offmins / 60;
$minutes = $offmins % 60;
$output .= sprintf("%s%02d:%02d", $direction, $hours, $minutes);
break;
case "p":
$output .= $this->hour >= 12 ? "pm" : "am";
break;
case "P":
$output .= $this->hour >= 12 ? "PM" : "AM";
break;
case "r":
$hour = ($this->hour + 1) > 12 ? $this->hour - 12 : $this->hour;
$output .= sprintf("%02d:%02d:%02d %s", $hour==0 ? 12 : $hour, $this->minute, $this->second, $this->hour >= 12 ? "PM" : "AM");
break;
case "R":
$output .= sprintf("%02d:%02d", $this->hour, $this->minute);
break;
case "S":
$output .= sprintf("%02d", $this->second);
break;
case "t":
$output .= "\t";
break;
case "T":
$output .= sprintf("%02d:%02d:%02d", $this->hour, $this->minute, $this->second);
break;
case "w":
$output .= Date_Calc::dayOfWeek($this->day,$this->month,$this->year);
break;
case "U":
$output .= Date_Calc::weekOfYear($this->day,$this->month,$this->year);
break;
case "y":
$output .= substr($this->year,2,2);
break;
case "Y":
$output .= $this->year;
break;
case "Z":
$output .= $this->tz->inDaylightTime($this) ? $this->tz->getDSTShortName() : $this->tz->getShortName();
break;
case "%":
$output .= "%";
break;
default:
$output .= $char.$nextchar;
}
$strpos++;
} else {
$output .= $char;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -