📄 date.php
字号:
return $output;
}
/**
* Get this date/time in Unix time() format
*
* Get a representation of this date in Unix time() format. This may only be
* valid for dates from 1970 to ~2038.
*
* @access public
* @return int number of seconds since the unix epoch
*/
function getTime()
{
return $this->getDate(DATE_FORMAT_UNIXTIME);
}
/**
* Sets the time zone of this Date
*
* Sets the time zone of this date with the given
* Date_TimeZone object. Does not alter the date/time,
* only assigns a new time zone. For conversion, use
* convertTZ().
*
* @access public
* @param object Date_TimeZone $tz the Date_TimeZone object to use, if called
* with a paramater that is not a Date_TimeZone object, will fall through to
* setTZbyID().
*/
function setTZ($tz)
{
if(is_a($tz, 'Date_Timezone')) {
$this->tz = $tz;
} else {
$this->setTZbyID($tz);
}
}
/**
* Sets the time zone of this date with the given time zone id
*
* Sets the time zone of this date with the given
* time zone id, or to the system default if the
* given id is invalid. Does not alter the date/time,
* only assigns a new time zone. For conversion, use
* convertTZ().
*
* @access public
* @param string id a time zone id
*/
function setTZbyID($id)
{
if (Date_TimeZone::isValidID($id)) {
$this->tz = new Date_TimeZone($id);
} else {
$this->tz = Date_TimeZone::getDefault();
}
}
/**
* Tests if this date/time is in DST
*
* Returns true if daylight savings time is in effect for
* this date in this date's time zone. See Date_TimeZone::inDaylightTime()
* for compatability information.
*
* @access public
* @return boolean true if DST is in effect for this date
*/
function inDaylightTime()
{
return $this->tz->inDaylightTime($this);
}
/**
* Converts this date to UTC and sets this date's timezone to UTC
*
* Converts this date to UTC and sets this date's timezone to UTC
*
* @access public
*/
function toUTC()
{
if ($this->tz->getOffset($this) > 0) {
$this->subtractSeconds(intval($this->tz->getOffset($this) / 1000));
} else {
$this->addSeconds(intval(abs($this->tz->getOffset($this)) / 1000));
}
$this->tz = new Date_TimeZone('UTC');
}
/**
* Converts this date to a new time zone
*
* Converts this date to a new time zone.
* WARNING: This may not work correctly if your system does not allow
* putenv() or if localtime() does not work in your environment. See
* Date::TimeZone::inDaylightTime() for more information.
*
* @access public
* @param object Date_TimeZone $tz the Date::TimeZone object for the conversion time zone
*/
function convertTZ($tz)
{
// convert to UTC
if ($this->tz->getOffset($this) > 0) {
$this->subtractSeconds(intval(abs($this->tz->getOffset($this)) / 1000));
} else {
$this->addSeconds(intval(abs($this->tz->getOffset($this)) / 1000));
}
// convert UTC to new timezone
if ($tz->getOffset($this) > 0) {
$this->addSeconds(intval(abs($tz->getOffset($this)) / 1000));
} else {
$this->subtractSeconds(intval(abs($tz->getOffset($this)) / 1000));
}
$this->tz = $tz;
}
/**
* Converts this date to a new time zone, given a valid time zone ID
*
* Converts this date to a new time zone, given a valid time zone ID
* WARNING: This may not work correctly if your system does not allow
* putenv() or if localtime() does not work in your environment. See
* Date::TimeZone::inDaylightTime() for more information.
*
* @access public
* @param string id a time zone id
*/
function convertTZbyID($id)
{
if (Date_TimeZone::isValidID($id)) {
$tz = new Date_TimeZone($id);
} else {
$tz = Date_TimeZone::getDefault();
}
$this->convertTZ($tz);
}
function toUTCbyOffset($offset)
{
if ($offset == "Z" || $offset == "+00:00" || $offset == "+0000") {
$this->toUTC();
return true;
}
if (preg_match('/([\+\-])(\d{2}):?(\d{2})/', $offset, $regs)) {
// convert offset to seconds
$hours = (int) isset($regs[2])?$regs[2]:0;
$mins = (int) isset($regs[3])?$regs[3]:0;
$offset = ($hours * 3600) + ($mins * 60);
if (isset($regs[1]) && $regs[1] == "-") {
$offset *= -1;
}
if ($offset > 0) {
$this->subtractSeconds(intval($offset));
} else {
$this->addSeconds(intval(abs($offset)));
}
$this->tz = new Date_TimeZone('UTC');
return true;
}
return false;
}
/**
* Adds a given number of seconds to the date
*
* Adds a given number of seconds to the date
*
* @access public
* @param int $sec the number of seconds to add
*/
function addSeconds($sec)
{
$this->addSpan(new Date_Span((integer)$sec));
}
/**
* Adds a time span to the date
*
* Adds a time span to the date
*
* @access public
* @param object Date_Span $span the time span to add
*/
function addSpan($span)
{
if (!is_a($span, 'Date_Span')) {
return;
}
$this->second += $span->second;
if ($this->second >= 60) {
$this->minute++;
$this->second -= 60;
}
$this->minute += $span->minute;
if ($this->minute >= 60) {
$this->hour++;
if ($this->hour >= 24) {
list($this->year, $this->month, $this->day) =
sscanf(Date_Calc::nextDay($this->day, $this->month, $this->year), "%04s%02s%02s");
$this->hour -= 24;
}
$this->minute -= 60;
}
$this->hour += $span->hour;
if ($this->hour >= 24) {
list($this->year, $this->month, $this->day) =
sscanf(Date_Calc::nextDay($this->day, $this->month, $this->year), "%04s%02s%02s");
$this->hour -= 24;
}
$d = Date_Calc::dateToDays($this->day, $this->month, $this->year);
$d += $span->day;
list($this->year, $this->month, $this->day) =
sscanf(Date_Calc::daysToDate($d), "%04s%02s%02s");
$this->year = intval($this->year);
$this->month = intval($this->month);
$this->day = intval($this->day);
}
/**
* Subtracts a given number of seconds from the date
*
* Subtracts a given number of seconds from the date
*
* @access public
* @param int $sec the number of seconds to subtract
*/
function subtractSeconds($sec)
{
$this->subtractSpan(new Date_Span($sec));
}
/**
* Subtracts a time span to the date
*
* Subtracts a time span to the date
*
* @access public
* @param object Date_Span $span the time span to subtract
*/
function subtractSpan($span)
{
if (!is_a($span, 'Date_Span')) {
return;
}
if ($span->isEmpty()) {
return;
}
$this->second -= $span->second;
if ($this->second < 0) {
$this->minute--;
$this->second += 60;
}
$this->minute -= $span->minute;
if ($this->minute < 0) {
$this->hour--;
if ($this->hour < 0) {
list($this->year, $this->month, $this->day) =
sscanf(Date_Calc::prevDay($this->day, $this->month, $this->year), "%04s%02s%02s");
$this->hour += 24;
}
$this->minute += 60;
}
$this->hour -= $span->hour;
if ($this->hour < 0) {
list($this->year, $this->month, $this->day) =
sscanf(Date_Calc::prevDay($this->day, $this->month, $this->year), "%04s%02s%02s");
$this->hour += 24;
}
$d = Date_Calc::dateToDays($this->day, $this->month, $this->year);
$d -= $span->day;
list($this->year, $this->month, $this->day) =
sscanf(Date_Calc::daysToDate($d), "%04s%02s%02s");
$this->year = intval($this->year);
$this->month = intval($this->month);
$this->day = intval($this->day);
}
/**
* Compares two dates
*
* Compares two dates. Suitable for use
* in sorting functions.
*
* @access public
* @param object Date $d1 the first date
* @param object Date $d2 the second date
* @return int 0 if the dates are equal, -1 if d1 is before d2, 1 if d1 is after d2
*/
function compare($d1, $d2)
{
$d1->convertTZ(new Date_TimeZone('UTC'));
$d2->convertTZ(new Date_TimeZone('UTC'));
$days1 = Date_Calc::dateToDays($d1->day, $d1->month, $d1->year);
$days2 = Date_Calc::dateToDays($d2->day, $d2->month, $d2->year);
if ($days1 < $days2) return -1;
if ($days1 > $days2) return 1;
if ($d1->hour < $d2->hour) return -1;
if ($d1->hour > $d2->hour) return 1;
if ($d1->minute < $d2->minute) return -1;
if ($d1->minute > $d2->minute) return 1;
if ($d1->second < $d2->second) return -1;
if ($d1->second > $d2->second) return 1;
return 0;
}
/**
* Test if this date/time is before a certain date/time
*
* Test if this date/time is before a certain date/time
*
* @access public
* @param object Date $when the date to test against
* @return boolean true if this date is before $when
*/
function before($when)
{
if (Date::compare($this,$when) == -1) {
return true;
} else {
return false;
}
}
/**
* Test if this date/time is after a certian date/time
*
* Test if this date/time is after a certian date/time
*
* @access public
* @param object Date $when the date to test against
* @return boolean true if this date is after $when
*/
function after($when)
{
if (Date::compare($this,$when) == 1) {
return true;
} else {
return false;
}
}
/**
* Test if this date/time is exactly equal to a certian date/time
*
* Test if this date/time is exactly equal to a certian date/time
*
* @access public
* @param object Date $when the date to test against
* @return boolean true if this date is exactly equal to $when
*/
function equals($when)
{
if (Date::compare($this,$when) == 0) {
return true;
} else {
return false;
}
}
/**
* Determine if this date is in the future
*
* Determine if this date is in the future
*
* @access public
* @return boolean true if this date is in the future
*/
function isFuture()
{
$now = new Date();
if ($this->after($now)) {
return true;
} else {
return false;
}
}
/**
* Determine if this date is in the past
*
* Determine if this date is in the past
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -