public function DateObject::__construct in Date 7
Same name and namespace in other branches
- 7.3 date_api/date_api.module \DateObject::__construct()
- 7.2 date_api/date_api.module \DateObject::__construct()
Overridden constructor.
Parameters
$time: time string, flexible format including timestamp.
$tz: PHP DateTimeZone object, string or NULL allowed, defaults to site timezone.
$format: PHP date() type format for parsing. Doesn't support timezones; if you have a timezone, send NULL and the default constructor method will hopefully parse it. $format is recommended in order to use negative or large years, which php's parser fails on.
1 call to DateObject::__construct()
- DateObject::__wakeup in date_api/
date_api.module - Upon unserializing, we must re-build ourselves using local variables.
File
- date_api/
date_api.module, line 96 - This module will make the date API available to other modules. Designed to provide a light but flexible assortment of functions and constants, with more functionality in additional files that are not loaded unless other modules specifically include them.
Class
- DateObject
- Extend PHP DateTime class with granularity handling, merge functionality and slightly more flexible initialization parameters.
Code
public function __construct($time = 'now', $tz = NULL, $format = NULL) {
$this->timeOnly = FALSE;
$this->dateOnly = FALSE;
// Allow string timezones
if (!empty($tz) && !is_object($tz)) {
$tz = new DateTimeZone($tz);
}
elseif (empty($tz)) {
$tz = date_default_timezone_object();
}
// Special handling for Unix timestamps expressed in the local timezone.
// Create a date object in UTC and convert it to the local timezone.
// Don't try to turn things like '2010' with a format of 'Y' into a timestamp.
if (is_numeric($time) && (empty($format) || $format == 'U')) {
// Assume timestamp.
$time = "@" . $time;
if ($tz
->getName() != 'UTC') {
$date = new DateObject($time, 'UTC');
$date
->setTimezone($tz);
$time = $date
->format(DATE_FORMAT_DATETIME);
$format = DATE_FORMAT_DATETIME;
}
}
if (is_array($time)) {
// Assume we were passed an indexed array.
if (empty($time['year']) && empty($time['month']) && empty($time['day'])) {
$this->timeOnly = TRUE;
}
if (empty($time['hour']) && empty($time['minute']) && empty($time['second'])) {
$this->dateOnly = TRUE;
}
$this->errors = $this
->arrayErrors($time);
// Make this into an ISO date,
// forcing a full ISO date even if some values are missing.
$time = $this
->toISO($time, TRUE);
// We checked for errors already, skip the step of parsing the input values.
$format = NULL;
}
// The parse function will also set errors on the date parts.
if (!empty($format)) {
$arg = self::$allgranularity;
$element = array_pop($arg);
while (!$this
->parse($time, $tz, $format) && $element != 'year') {
$element = array_pop($arg);
$format = date_limit_format($format, $arg);
}
if ($element == 'year') {
return FALSE;
}
}
elseif (is_string($time)) {
// PHP < 5.3 doesn't like the GMT- notation for parsing timezones.
$time = str_replace("GMT-", "-", $time);
$time = str_replace("GMT+", "+", $time);
// We are going to let the parent dateObject do a best effort attempt to turn this
// string into a valid date. It might fail and we want to control the error messages.
try {
@parent::__construct($time, $tz);
} catch (Exception $e) {
$this->errors['date'] = $e;
return;
}
$this
->setGranularityFromTime($time, $tz);
}
// This tz was given as just an offset, which causes problems,
// or the timezone was invalid.
if (!$this
->getTimezone() || !preg_match('/[a-zA-Z]/', $this
->getTimezone()
->getName())) {
$this
->setTimezone(new DateTimeZone("UTC"));
}
}