DrupalDateTime
class DrupalDateTime extends DateTimePlus (View source)
Extends DateTimePlus().
This class extends the basic component and adds in Drupal-specific handling, like translation of the format() method.
Static methods in base class can also be used to create DrupalDateTime objects. For example:
DrupalDateTime::createFromArray( array('year' => 2010, 'month' => 9, 'day' => 28) )
Traits
Wrapper methods for \Drupal\Core\StringTranslation\TranslationInterface.
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. |
from DateTimePlus |
| protected string | $inputTimeRaw | The value of the time value passed to the constructor. |
from DateTimePlus |
| protected string | $inputTimeAdjusted | The prepared time, without timezone, for this date. |
from DateTimePlus |
| protected string | $inputTimeZoneRaw | The value of the timezone passed to the constructor. |
from DateTimePlus |
| protected string | $inputTimeZoneAdjusted | The prepared timezone object used to construct this date. |
from DateTimePlus |
| protected string | $inputFormatRaw | The value of the format passed to the constructor. |
from DateTimePlus |
| protected string | $inputFormatAdjusted | The prepared format, if provided. |
from DateTimePlus |
| protected | $langcode | The value of the language code passed to the constructor. |
from DateTimePlus |
| protected | $errors | An array of errors encountered when creating this date. |
from DateTimePlus |
| protected DateTime | $dateTimeObject | The DateTime object. |
from DateTimePlus |
| protected TranslationInterface | $stringTranslation | The string translation service. |
from StringTranslationTrait |
| protected array | $formatTranslationCache | Formatted strings translation cache. |
Methods
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.
Returns the difference between two DateTimePlus objects.
Overrides prepareTimezone().
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.
Overrides format().
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
Translates a string to the current language or to a given language.
Formats a string containing a count of items.
Returns the number of plurals supported by a given language.
Gets the string translation service.
Details
__toString()
Implements the magic __toString() method.
protected
_die()
For test purposes, wrap die() in an overridable method.
abstract string|object
render()
Renders the object as a string.
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.
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)
Overrides prepareTimezone().
Override basic component timezone handling to use Drupal's knowledge of the preferred user timezone.
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 = [])
Overrides format().
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
protected TranslatableMarkup
t(string $string, array $args = [], array $options = [])
Translates a string to the current language or to a given language.
See \Drupal\Core\StringTranslation\TranslatableMarkup::__construct() for important security information and usage guidelines.
In order for strings to be localized, make them available in one of the ways supported by the
protected
formatPlural($count, $singular, $plural, array $args = [], array $options = [])
Formats a string containing a count of items.
protected
getNumberOfPlurals($langcode = NULL)
Returns the number of plurals supported by a given language.
protected TranslationInterface
getStringTranslation()
Gets the string translation service.
$this
setStringTranslation(TranslationInterface $translation)
Sets the string translation service to use.