class DateTimePlus (View source)

Wraps DateTime().

This class wraps the PHP DateTime class with more flexible initialization parameters, allowing a date to be created from an existing date object, a timestamp, a string with an unknown format, a string with a known format, or an array of date parts. It also adds an errors array and a __toString() method to the date object.

This class is less lenient than the DateTime class. It changes the default behavior for handling date values like '2011-00-00'. The DateTime class would convert that value to '2010-11-30' and report a warning but not an error. This extension treats that as an error.

As with the DateTime class, a date object may be created even if it has errors. It has an errors array attached to it that explains what the errors are. This is less disruptive than allowing datetime exceptions to abort processing. The calling script can decide what to do about errors using hasErrors() and getErrors().

Traits

Wraps __toString in a trait to avoid some fatals.

Constants

FORMAT

RFC7231

A RFC7231 Compliant date.

Properties

static protected $dateParts

An array of possible date parts.

protected string $inputTimeRaw

The value of the time value passed to the constructor.

protected string $inputTimeAdjusted

The prepared time, without timezone, for this date.

protected string $inputTimeZoneRaw

The value of the timezone passed to the constructor.

protected string $inputTimeZoneAdjusted

The prepared timezone object used to construct this date.

protected string $inputFormatRaw

The value of the format passed to the constructor.

protected string $inputFormatAdjusted

The prepared format, if provided.

protected $langcode

The value of the language code passed to the constructor.

protected $errors

An array of errors encountered when creating this date.

protected DateTime $dateTimeObject

The DateTime object.

Methods

__toString()

Implements the magic __toString() method.

_die()

For test purposes, wrap die() in an overridable method.

string
render()

Renders the timezone name.

static DateTimePlus
createFromDateTime(DateTime $datetime, array $settings = [])

Creates a date object from an input date object.

static DateTimePlus
createFromArray(array $date_parts, mixed $timezone = NULL, array $settings = [])

Creates a date object from an array of date parts.

static DateTimePlus
createFromTimestamp(int $timestamp, mixed $timezone = NULL, array $settings = [])

Creates a date object from timestamp input.

static DateTimePlus
createFromFormat(string $format, string $time, mixed $timezone = NULL, array $settings = [])

Creates a date object from an input format.

__construct(string $time = 'now', mixed $timezone = NULL, array $settings = [])

Constructs a date object set to a requested date and timezone.

mixed
__call(string $method, array $args)

Implements the magic __call method.

diff(DateTimePlus|DateTime $datetime2, bool $absolute = FALSE)

Returns the difference between two DateTimePlus objects.

static 
__callStatic($method, $args)

Implements the magic __callStatic method.

__clone()

Implements the magic __clone method.

mixed
prepareTime(mixed $time)

Prepares the input time value.

prepareTimezone(mixed $timezone)

Prepares the input timezone value.

string
prepareFormat(string $format)

Prepares the input format value.

checkErrors()

Examines getLastErrors() to see what errors to report.

bool
hasErrors()

Detects if there were errors in the processing of this date.

array
getErrors()

Gets error messages.

static string
arrayToISO(array $array, bool $force_valid_date = FALSE)

Creates an ISO date from an array of values.

static array
prepareArray(array $array, bool $force_valid_date = FALSE)

Creates a complete array from a possibly incomplete array of date parts.

static bool
checkArray(array $array)

Checks that arrays of date parts will create a valid date.

static string
datePad(int $value, int $size = 2)

Pads date parts with zeros.

string|null
format(string $format, array $settings = [])

Formats the date for display.

setDefaultDateTime()

Sets the default time for an object built from date-only data.

getPhpDateTime()

Gets a clone of the proxied PHP \DateTime object wrapped by this class.

$this
add(DateInterval $interval)

No description

static array
getLastErrors()

No description

$this
modify(string $modify)

No description

$this
setDate(int $year, int $month, int $day)

No description

$this
setISODate(int $year, int $week, int $day = 1)

No description

$this
setTime(int $hour, int $minute, int $second, int $microseconds)

No description

$this
setTimestamp(int $unixtimestamp)

No description

$this
setTimezone(DateTimeZone $timezone)

No description

$this
sub(DateInterval $interval)

No description

int
getOffset()

No description

int
getTimestamp()

No description

getTimezone()

No description

Details

__toString()

Implements the magic __toString() method.

protected _die()

For test purposes, wrap die() in an overridable method.

string render()

Renders the timezone name.

Return Value

string

static DateTimePlus createFromDateTime(DateTime $datetime, array $settings = [])

Creates a date object from an input date object.

Parameters

DateTime $datetime

A DateTime object.

array $settings

(optional) A keyed array for settings, suitable for passing on to __construct().

Return Value

DateTimePlus

A new DateTimePlus object.

static DateTimePlus createFromArray(array $date_parts, mixed $timezone = NULL, array $settings = [])

Creates a date object from an array of date parts.

Converts the input value into an ISO date, forcing a full ISO date even if some values are missing.

Parameters

array $date_parts

An array of date parts, like ('year' => 2014, 'month' => 4).

mixed $timezone

(optional) \DateTimeZone object, time zone string or NULL. NULL uses the default system time zone. Defaults to NULL.

array $settings

(optional) A keyed array for settings, suitable for passing on to __construct().

Return Value

DateTimePlus

A new DateTimePlus object.

Exceptions

InvalidArgumentException

static DateTimePlus createFromTimestamp(int $timestamp, mixed $timezone = NULL, array $settings = [])

Creates a date object from timestamp input.

The timezone of a timestamp is always UTC. The timezone for a timestamp indicates the timezone used by the format() method.

Parameters

int $timestamp

A UNIX timestamp.

mixed $timezone

(optional) \DateTimeZone object, time zone string or NULL. See __construct() for more details.

array $settings

(optional) A keyed array for settings, suitable for passing on to __construct().

Return Value

DateTimePlus

A new DateTimePlus object.

Exceptions

InvalidArgumentException

static DateTimePlus createFromFormat(string $format, string $time, mixed $timezone = NULL, array $settings = [])

Creates a date object from an input format.

Parameters

string $format

PHP date() type format for parsing the input. This is recommended to use things like negative years, which php's parser fails on, or any other specialized input with a known format. If provided the date will be created using the createFromFormat() method. http://php.net/manual/datetime.createfromformat.php

string $time

String representing the time.

mixed $timezone

(optional) \DateTimeZone object, time zone string or NULL. See __construct() for more details.

array $settings

(optional) A keyed array for settings, suitable for passing on to __construct(). Supports an additional key:

  • validate_format: (optional) Boolean choice to validate the created date using the input format. The format used in createFromFormat() allows slightly different values than format(). Using an input format that works in both functions makes it possible to a validation step to confirm that the date created from a format string exactly matches the input. This option indicates the format can be used for validation. Defaults to TRUE.

Return Value

DateTimePlus

A new DateTimePlus object.

Exceptions

InvalidArgumentException
UnexpectedValueException

__construct(string $time = 'now', mixed $timezone = NULL, array $settings = [])

Constructs a date object set to a requested date and timezone.

Parameters

string $time

(optional) A date/time string. Defaults to 'now'.

mixed $timezone

(optional) \DateTimeZone object, time zone string or NULL. NULL uses the default system time zone. Defaults to NULL. Note that the $timezone parameter and the current timezone are ignored when the $time parameter either is a UNIX timestamp (e.g. @946684800) or specifies a timezone (e.g. 2010-01-28T15:00:00+02:00). http://php.net/manual/datetime.construct.php

array $settings

(optional) Keyed array of settings. Defaults to empty array.

  • langcode: (optional) String two letter language code used to control the result of the format(). Defaults to NULL.
  • debug: (optional) Boolean choice to leave debug values in the date object for debugging purposes. Defaults to FALSE.

mixed __call(string $method, array $args)

Implements the magic __call method.

Passes through all unknown calls onto the DateTime object.

Parameters

string $method

The method to call on the decorated object.

array $args

Call arguments.

Return Value

mixed

The return value from the method on the decorated object. If the proxied method call returns a DateTime object, then return the original DateTimePlus object, which allows function chaining to work properly. Otherwise, the value from the proxied method call is returned.

Exceptions

Exception
BadMethodCallException

DateInterval diff(DateTimePlus|DateTime $datetime2, bool $absolute = FALSE)

Returns the difference between two DateTimePlus objects.

Parameters

DateTimePlus|DateTime $datetime2

The date to compare to.

bool $absolute

Should the interval be forced to be positive?

Return Value

DateInterval

A DateInterval object representing the difference between the two dates.

Exceptions

BadMethodCallException

static __callStatic($method, $args)

Implements the magic __callStatic method.

Passes through all unknown static calls onto the DateTime object.

Parameters

$method
$args

__clone()

Implements the magic __clone method.

Deep-clones the DateTime object we're wrapping.

protected mixed prepareTime(mixed $time)

Prepares the input time value.

Changes the input value before trying to use it, if necessary. Can be overridden to handle special cases.

Parameters

mixed $time

An input value, which could be a timestamp, a string, or an array of date parts.

Return Value

mixed

The massaged time.

protected DateTimeZone prepareTimezone(mixed $timezone)

Prepares the input timezone value.

Changes the timezone before trying to use it, if necessary. Most importantly, makes sure there is a valid timezone object before moving further.

Parameters

mixed $timezone

Either a timezone name or a timezone object or NULL.

Return Value

DateTimeZone

The massaged time zone.

protected string prepareFormat(string $format)

Prepares the input format value.

Changes the input format before trying to use it, if necessary. Can be overridden to handle special cases.

Parameters

string $format

A PHP format string.

Return Value

string

The massaged PHP format string.

checkErrors()

Examines getLastErrors() to see what errors to report.

Two kinds of errors are important: anything that DateTime considers an error, and also a warning that the date was invalid. PHP creates a valid date from invalid data with only a warning, 2011-02-30 becomes 2011-03-03, for instance, but we don't want that.

bool hasErrors()

Detects if there were errors in the processing of this date.

Return Value

bool

TRUE if there were errors in the processing of this date, FALSE otherwise.

array getErrors()

Gets error messages.

Public function to return the error messages.

Return Value

array

An array of errors encountered when creating this date.

static string arrayToISO(array $array, bool $force_valid_date = FALSE)

Creates an ISO date from an array of values.

Parameters

array $array

An array of date values keyed by date part.

bool $force_valid_date

(optional) Whether to force a full date by filling in missing values. Defaults to FALSE.

Return Value

string

The date as an ISO string.

static array prepareArray(array $array, bool $force_valid_date = FALSE)

Creates a complete array from a possibly incomplete array of date parts.

Parameters

array $array

An array of date values keyed by date part.

bool $force_valid_date

(optional) Whether to force a valid date by filling in missing values with valid values or just to use empty values instead. Defaults to FALSE.

Return Value

array

A complete array of date parts.

static bool checkArray(array $array)

Checks that arrays of date parts will create a valid date.

Checks that an array of date parts has a year, month, and day, and that those values create a valid date. If time is provided, verifies that the time values are valid. Sort of an equivalent to checkdate().

Parameters

array $array

An array of datetime values keyed by date part.

Return Value

bool

TRUE if the datetime parts contain valid values, otherwise FALSE.

static string datePad(int $value, int $size = 2)

Pads date parts with zeros.

Helper function for a task that is often required when working with dates.

Parameters

int $value

The value to pad.

int $size

(optional) Size expected, usually 2 or 4. Defaults to 2.

Return Value

string

The padded value.

string|null format(string $format, array $settings = [])

Formats the date for display.

Parameters

string $format

Format accepted by date().

array $settings
  • timezone: (optional) String timezone name. Defaults to the timezone of the date object.

Return Value

string|null

The formatted value of the date or NULL if there were construction errors.

setDefaultDateTime()

Sets the default time for an object built from date-only data.

The default time for a date without time can be anything, so long as it is consistently applied. If we use noon, dates in most timezones will have the same value for in both the local timezone and UTC.

DateTime getPhpDateTime()

Gets a clone of the proxied PHP \DateTime object wrapped by this class.

Return Value

DateTime

A clone of the wrapped PHP \DateTime object.

$this add(DateInterval $interval)

No description

Parameters

DateInterval $interval

Return Value

$this

static array getLastErrors()

No description

Return Value

array

$this modify(string $modify)

No description

Parameters

string $modify

Return Value

$this

$this setDate(int $year, int $month, int $day)

No description

Parameters

int $year
int $month
int $day

Return Value

$this

$this setISODate(int $year, int $week, int $day = 1)

No description

Parameters

int $year
int $week
int $day

Return Value

$this

$this setTime(int $hour, int $minute, int $second, int $microseconds)

No description

Parameters

int $hour
int $minute
int $second
int $microseconds

Return Value

$this

$this setTimestamp(int $unixtimestamp)

No description

Parameters

int $unixtimestamp

Return Value

$this

$this setTimezone(DateTimeZone $timezone)

No description

Parameters

DateTimeZone $timezone

Return Value

$this

$this sub(DateInterval $interval)

No description

Parameters

DateInterval $interval

Return Value

$this

int getOffset()

No description

Return Value

int

int getTimestamp()

No description

Return Value

int

DateTimeZone getTimezone()

No description

Return Value

DateTimeZone