DateTimeConverter extends AbstractTypeConverter

Converter which transforms from different input formats into DateTime objects.

Source can be either a string or an array. The date string is expected to be formatted according to DEFAULT_DATE_FORMAT.

But the default date format can be overridden in the initialize*Action() method like this::

$this->arguments['<argumentName>'] ->getPropertyMappingConfiguration() ->forProperty('<propertyName>') // this line can be skipped in order to specify the format for all properties ->setTypeConverterOption(\TYPO3\CMS\Extbase\Property\TypeConverter\DateTimeConverter::class, \TYPO3\CMS\Extbase\Property\TypeConverter\DateTimeConverter::CONFIGURATION_DATE_FORMAT, '<dateFormat>');

If the source is of type array, it is possible to override the format in the source::

array( 'date' => '<dateString>', 'dateFormat' => '<dateFormat>' );

By using an array as source you can also override time and timezone of the created DateTime object::

array( 'date' => '<dateString>', 'hour' => '', // integer 'minute' => '', // integer 'seconds' => '', // integer 'timezone' => '', // string, see http://www.php.net/manual/timezones.php );

As an alternative to providing the date as string, you might supply day, month and year as array items each::

array( 'day' => '', // integer 'month' => '', // integer 'year' => '', // integer );

Table of Contents

Constants

CONFIGURATION_DATE_FORMAT  = 'dateFormat'
DEFAULT_DATE_FORMAT  = \DateTimeInterface::W3C
The default date format is "YYYY-MM-DDT##:##:##+##:##", for example "2005-08-15T15:52:01+00:00" according to the W3C standard @see http://www.w3.org/TR/NOTE-datetime.html

Properties

$priority  : int
The priority for this converter.
$sourceTypes  : array<string|int, string>
The source types this converter can convert.
$targetType  : string
The target type this converter can convert to.

Methods

canConvertFrom()  : bool
If conversion is possible.
convertFrom()  : DateTime|Error|null
Converts $source to a \DateTime using the configured dateFormat
getPriority()  : int
Return the priority of this TypeConverter. TypeConverters with a high priority are chosen before low priority.
getSourceChildPropertiesToBeConverted()  : array<string|int, mixed>
Return a list of sub-properties inside the source object.
getSupportedSourceTypes()  : array<string|int, string>
Returns the list of source types the TypeConverter can handle.
getSupportedTargetType()  : string
Return the target type this TypeConverter converts to.
getTargetTypeForSource()  : string
Returns the type for a given source, depending on e.g. the __type setting or other properties.
getTypeOfChildProperty()  : string
Return the type of a given sub-property inside the $targetType
getDefaultDateFormat()  : string
Determines the default date format to use for the conversion.
isDatePartKeysProvided()  : bool
Returns whether date information (day, month, year) are present as keys in $source.
overrideTimeIfSpecified()  : DateTime
Overrides hour, minute & second of the given date with the values in the $source array

Constants

CONFIGURATION_DATE_FORMAT

public string CONFIGURATION_DATE_FORMAT = 'dateFormat'

DEFAULT_DATE_FORMAT

The default date format is "YYYY-MM-DDT##:##:##+##:##", for example "2005-08-15T15:52:01+00:00" according to the W3C standard @see http://www.w3.org/TR/NOTE-datetime.html

public string DEFAULT_DATE_FORMAT = \DateTimeInterface::W3C

Properties

$priority

The priority for this converter.

Deprecated

will be removed in TYPO3 v13.0, as this is defined in Services.yaml.

protected int $priority = 10

$sourceTypes

The source types this converter can convert.

Deprecated

will be removed in TYPO3 v13.0, as this is defined in Services.yaml.

protected array<string|int, string> $sourceTypes = ['string', 'integer', 'array']

$targetType

The target type this converter can convert to.

Deprecated

will be removed in TYPO3 v13.0, as this is defined in Services.yaml.

protected string $targetType = \DateTime::class

Methods

canConvertFrom()

If conversion is possible.

public canConvertFrom(string|array<string|int, mixed>|int $source, string $targetType) : bool
Parameters
$source : string|array<string|int, mixed>|int
$targetType : string
Internal

only to be used within Extbase, not part of TYPO3 Core API.

Return values
bool

convertFrom()

Converts $source to a \DateTime using the configured dateFormat

public convertFrom(string|int|array<string|int, mixed> $source, string $targetType[, array<string|int, mixed> $convertedChildProperties = [] ][, PropertyMappingConfigurationInterface $configuration = null ]) : DateTime|Error|null
Parameters
$source : string|int|array<string|int, mixed>

the string to be converted to a \DateTime object

$targetType : string

must be "DateTime"

$convertedChildProperties : array<string|int, mixed> = []

not used currently

$configuration : PropertyMappingConfigurationInterface = null
Internal

only to be used within Extbase, not part of TYPO3 Core API.

Tags
throws
TypeConverterException
Return values
DateTime|Error|null

getPriority()

Return the priority of this TypeConverter. TypeConverters with a high priority are chosen before low priority.

public getPriority() : int
Deprecated

will be removed in TYPO3 v13.0, as this is defined in Services.yaml.

Return values
int

getSourceChildPropertiesToBeConverted()

Return a list of sub-properties inside the source object.

public getSourceChildPropertiesToBeConverted(mixed $source) : array<string|int, mixed>
Parameters
$source : mixed
Tags
todo

this method is only used for converter sources that have children (i.e. objects). Introduce another ChildPropertyAwareTypeConverterInterface and drop this method from the main interface

Returns an empty list of sub property names

Return values
array<string|int, mixed>

getSupportedSourceTypes()

Returns the list of source types the TypeConverter can handle.

public getSupportedSourceTypes() : array<string|int, string>
Deprecated

will be removed in TYPO3 v13.0, as this is defined in Services.yaml.

Must be PHP simple types, classes or object is not allowed.

Return values
array<string|int, string>

getSupportedTargetType()

Return the target type this TypeConverter converts to.

public getSupportedTargetType() : string
Deprecated

will be removed in TYPO3 v13.0, as this is defined in Services.yaml.

Can be a simple type or a class name.

Return values
string

getTargetTypeForSource()

Returns the type for a given source, depending on e.g. the __type setting or other properties.

public getTargetTypeForSource(mixed $source, string $originalTargetType[, PropertyMappingConfigurationInterface|null $configuration = null ]) : string
Parameters
$source : mixed

the source data

$originalTargetType : string

the type we originally want to convert to

$configuration : PropertyMappingConfigurationInterface|null = null
Tags
todo

The concept of this method is flawed because it enables the override of the target type depending on the structure of the source. So, technically we no longer convert type A to B but source of type A with structure X to type B defined by X. This makes a type converter non-deterministic.

Returns the $originalTargetType unchanged in this implementation.

Return values
string

getTypeOfChildProperty()

Return the type of a given sub-property inside the $targetType

public getTypeOfChildProperty(string $targetType, string $propertyName, PropertyMappingConfigurationInterface $configuration) : string
Parameters
$targetType : string
$propertyName : string
$configuration : PropertyMappingConfigurationInterface
Tags
todo

this method is only used for converter sources that have children (i.e. objects). Introduce another ChildPropertyAwareTypeConverterInterface and drop this method from the main interface

This method is never called, as getSourceChildPropertiesToBeConverted() returns an empty array.

Return values
string

the type of $propertyName in $targetType

isDatePartKeysProvided()

Returns whether date information (day, month, year) are present as keys in $source.

protected isDatePartKeysProvided(array<string|int, mixed> $source) : bool
Parameters
$source : array<string|int, mixed>
Return values
bool

overrideTimeIfSpecified()

Overrides hour, minute & second of the given date with the values in the $source array

protected overrideTimeIfSpecified(DateTime $date, array<string|int, mixed> $source) : DateTime
Parameters
$date : DateTime
$source : array<string|int, mixed>
Return values
DateTime

        
On this page

Search results