1 date.class.inc | public BackdropDateTime::__construct($time = 'now', $tz = NULL, $format = NULL) |
Constructs a date object.
Parameters
string $time: A date/time string or array. Defaults to 'now'.
object|string|null $tz: PHP DateTimeZone object, string or NULL allowed. Defaults to NULL.
string $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, on which PHP's parser fails.
File
- core/
includes/ date.class.inc, line 63
Class
- BackdropDateTime
- Extends PHP DateTime class for use with Backdrop.
Code
public function __construct($time = 'now', $tz = NULL, $format = NULL) {
$this->timeOnly = FALSE;
$this->dateOnly = FALSE;
// Store the raw time input so it is available for validation.
$this->originalTime = $time;
// Allow string timezones.
if (!empty($tz) && !is_object($tz)) {
$tz = new DateTimeZone($tz);
}
// Default to the site timezone when not explicitly provided.
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;
$date = new BackdropDateTime($time, 'UTC');
if ($tz->getName() != 'UTC') {
$date->setTimezone($tz);
}
$time = $date->format(DATE_FORMAT_DATETIME);
$format = DATE_FORMAT_DATETIME;
$this->addGranularity('timezone');
}
elseif (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 parsing the input values.
$format = NULL;
}
else {
// Make sure dates like 2010-00-00T00:00:00 get converted to
// 2010-01-01T00:00:00 before creating a date object
// to avoid unintended changes in the month or day.
$time = date_make_iso_valid($time);
}
// The parse function will also set errors on the date parts.
$successfully_parsed = FALSE;
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);
}
$successfully_parsed = empty($this->errors);
// Remove the invalid error so it can be tried again by PHP's parser.
if (isset($this->errors['invalid'])) {
unset($this->errors['invalid']);
}
}
if (!$successfully_parsed && is_string($time)) {
// Remove superfluous "GMT" from offsets in timezones.
$time = str_replace("GMT-", "-", $time);
$time = str_replace("GMT+", "+", $time);
// We are going to let the parent DateTime object do a best effort attempt
// to turn this string into a valid date. It might fail and we want to
// control the error messages.
if (strtotime($time) !== FALSE) {
parent::__construct($time, $tz);
}
else {
$this->errors['date'] = t('The date "!date" does not match the expected format.', array('!date' => $time));
return;
}
if (empty($this->granularity)) {
$this->setGranularityFromTime($time, $tz);
}
}
// If we haven't got a valid timezone name yet, we need to set one or
// we will get undefined index errors.
// This can happen if $time had an offset or no timezone.
if (!$this->getTimezone() || !preg_match('/[a-zA-Z]/', $this->getTimezone()->getName())) {
// If the original $tz has a name, use it.
if (preg_match('/[a-zA-Z]/', $tz->getName())) {
$this->setTimezone($tz);
}
// We have no information about the timezone so must fallback to a default.
else {
$this->setTimezone(new DateTimeZone("UTC"));
$this->errors['timezone'] = t('No valid timezone name was provided.');
}
}
}