date.php
来自「groupoffice」· PHP 代码 · 共 1,228 行 · 第 1/3 页
PHP
1,228 行
$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; } } 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 * go_date_TimeZone object. Does not alter the date/time, * only assigns a new time zone. For conversion, use * convertTZ(). * * @access public * @param object go_date_TimeZone $tz the go_date_TimeZone object to use, if called * with a paramater that is not a go_date_TimeZone object, will fall through to * setTZbyID(). */ function setTZ($tz) { if(is_object($tz)) { $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 (go_date_TimeZone::isValidID($id)) { $this->tz = new go_date_TimeZone($id); } else { $this->tz = go_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 go_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 go_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 go_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 (go_date_TimeZone::isValidID($id)) { $tz = new go_date_TimeZone($id); } else { $tz = go_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 go_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 go_date_Span((integer)$sec)); } /** * Adds a time span to the date * * Adds a time span to the date * * @access public * @param object go_date_Span $span the time span to add */ function addSpan($span) { if (!is_object($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(go_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(go_date_Calc::nextDay($this->day, $this->month, $this->year), "%04s%02s%02s"); $this->hour -= 24; } $d = go_date_Calc::dateToDays($this->day, $this->month, $this->year); $d += $span->day; list($this->year, $this->month, $this->day) = sscanf(go_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 go_date_Span($sec)); } /** * Subtracts a time span to the date * * Subtracts a time span to the date * * @access public * @param object go_date_Span $span the time span to subtract */ function subtractSpan($span) { if (!is_object($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(go_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(go_date_Calc::prevDay($this->day, $this->month, $this->year), "%04s%02s%02s"); $this->hour += 24; } $d = go_date_Calc::dateToDays($this->day, $this->month, $this->year); $d -= $span->day; list($this->year, $this->month, $this->day) = sscanf(go_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 go_date_TimeZone('UTC')); $d2->convertTZ(new go_date_TimeZone('UTC')); $days1 = go_date_Calc::dateToDays($d1->day, $d1->month, $d1->year); $days2 = go_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;
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?