DateTimePlus
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
Renders the timezone name.
Creates a date object from an input date object.
Creates a date object from an array of date parts.
Creates a date object from timestamp input.
Creates a date object from an input format.
Constructs a date object set to a requested date and timezone.
Implements the magic __call method.
Returns the difference between two DateTimePlus objects.
Implements the magic __callStatic method.
Implements the magic __clone method.
Prepares the input time value.
Prepares the input timezone value.
Prepares the input format value.
Examines getLastErrors() to see what errors to report.
Detects if there were errors in the processing of this date.
Gets error messages.
Creates an ISO date from an array of values.
Creates a complete array from a possibly incomplete array of date parts.
Checks that arrays of date parts will create a valid date.
Pads date parts with zeros.
Formats the date for display.
Sets the default time for an object built from date-only data.
Gets a clone of the proxied PHP \DateTime object wrapped by this class.
No description
No description
No description
No description
No description
No description
No description
No description
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.
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.
Converts the input value into an ISO date, forcing a full ISO date even if some values are missing.
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.
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.
Passes through all unknown calls onto the DateTime object.
DateInterval
diff(DateTimePlus|DateTime $datetime2, bool $absolute = FALSE)
Returns the difference between two DateTimePlus objects.
static
__callStatic($method, $args)
Implements the magic __callStatic method.
Passes through all unknown static calls onto the DateTime object.
__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.
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.
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.
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.
array
getErrors()
Gets error messages.
Public function to return the 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.
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().
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.
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.
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.
$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
DateTimeZone
getTimezone()
No description