public static function createFromTimestamp($time) { $self = new self(); $self->setTimestamp($time); $self->setTimezone(self::_getDefaultTimeZone()); return $self; }
public static function fromGmtTimestamp($timestamp, $timezone = null) { if (is_null($timezone)) { $timezone = self::defaultTimezone(); } $date_time = new self("@{$timestamp}", new DateTimeZone('UTC')); $date_time->setTimezone($timezone); return $date_time; }
/** * Create DateTime object defaults to something that will accept * the default MySQL datetime format of "Y-m-d H:i:s". * * @param string $formatOrTime * @param string $time -OPTIONAL * @param DateTimeZone $timezone -OPTIONAL * @return DateTime */ public static function createFromFormat($formatOrTime, $time = '', $timezone = null) { if ($time === '') { $parsed = date_parse($formatOrTime); } else { $parsed = date_parse_from_format($formatOrTime, $time); } $d = new self(); if ($timezone !== null) { $d->setTimezone($timezone); } $d->setDate($parsed); $d->setTime($parsed); return $d; }
/** * Parse a string into a new DateTime object according to the specified format * * @param string $format Format accepted by date(). * @param string $time String representing the time. * @param \DateTimeZone $timezone A DateTimeZone object representing the desired time zone. * * @throws \Exception * * @return $this|self * * @link http://php.net/manual/en/datetime.createfromformat.php */ public static function createFromFormat($format, $time, $timezone = null) { if ($timezone) { $datetime = date_create_from_format($format, $time, $timezone); } else { $datetime = date_create_from_format($format, $time, new \DateTimeZone(date_default_timezone_get())); } if (!$datetime) { throw new \Exception(sprintf("Invalid datetime object created from '%s' with format '%s'", $format, $time)); } $return = new self(); $return->setTimestamp($datetime->getTimestamp()); $return->setTimezone(new \DateTimeZone(date_default_timezone_get())); return $return; }
/** * Returns the calculated date * * @param string $calc Calculation to make * @param string|integer|array|Zend_Date $date Date to calculate with, if null the actual date is taken * @param string $format Date format for parsing * @param string|Zend_Locale $locale Locale for parsing input * @return integer|Zend_Date new date * @throws Zend_Date_Exception */ private function _date($calc, $date, $format, $locale) { if (is_null($date)) { require_once 'Zend/Date/Exception.php'; throw new Zend_Date_Exception('parameter $date must be set, null is not allowed'); } if ($locale === null) { $locale = $this->getLocale(); } if ($date instanceof Zend_Date) { // extract date from object $date = $date->get(self::DATE_FULL, $locale); } else { if (is_array($date)) { if (isset($time['year']) === true or isset($time['month']) === true or isset($time['day']) === true) { $parsed = $time; } else { require_once 'Zend/Date/Exception.php'; throw new Zend_Date_Exception("no day,month or year given in array"); } } else { if (self::$_options['format_type'] == 'php') { $format = Zend_Locale_Format::convertPhpToIsoFormat($format); } try { $parsed = Zend_Locale_Format::getDate($date, array('date_format' => $format, 'locale' => $locale, 'format_type' => 'iso')); if (strpos(strtoupper($format), 'YY') !== false and strpos(strtoupper($format), 'YYYY') === false) { $parsed['year'] = self::getFullYear($parsed['year']); } } catch (Zend_Locale_Exception $e) { require_once 'Zend/Date/Exception.php'; throw new Zend_Date_Exception($e->getMessage()); } } $date = new self(0, self::TIMESTAMP, $locale); $date->setTimezone('UTC'); $date->set($parsed['year'], self::YEAR); $date->set($parsed['month'], self::MONTH); $date->set($parsed['day'], self::DAY); $date = $date->get(self::DATE_FULL, $locale); } $return = $this->_calcdetail($calc, $date, self::DATE_FULL, $locale); if ($calc != 'cmp') { return $this; } return $return; }
/** * Get a timestamp instance in the server local timezone ($wgLocaltimezone) * * @since 1.22 * @param bool|string $ts Timestamp to set, or false for current time * @return MWTimestamp The local instance */ public static function getLocalInstance($ts = false) { global $wgLocaltimezone; $timestamp = new self($ts); $timestamp->setTimezone($wgLocaltimezone); return $timestamp; }
/** * Implement some get_ methods that fetch variables * * @param string $name * @param array $args * @return mixed */ public function __call($name, $args) { // fill in 5.2.x gaps if ($name == "getTimestamp") { return $this->format('U') + 0; } if ($name == "setTimestamp") { $sec = (int) $args[0]; $sd = new self("@{$sec}"); $sd->setTimezone($this->getTimezone()); return $sd; } // getters if (substr($name, 0, 4) == "get_") { $var = substr($name, 4); if (isset($this->var_gets[$var])) { return $this->__get($this->var_gets[$var]); } if (isset($this->formats[$var])) { return $this->__get($var); } } $GLOBALS['log']->fatal("SugarDateTime: unknowm method {$name} called"); sugar_die("SugarDateTime: unknowm method {$name} called"); return false; }
/** * {@inheritdoc} */ public static function fromArray(array $values) { $message = new self(); $values = array_merge(['country' => null, 'language' => null, 'timezone' => null], $values); $message->setCountry($values['country']); $message->setLanguage($values['language']); $message->setTimezone($values['timezone']); return $message; }