W3cubDocs

/CakePHP 4.1

Class DateTimeType

Datetime type converter.

Use to convert datetime instances to strings & back.

Namespace: Cake\Database\Type

Properties summary

  • $_className protected
    string

    The classname to use when creating objects.

  • $_format protected
    string

    The DateTime format used when converting to string.

  • string|array|int

    The locale-aware format marshal() uses when _useLocaleParser is true.

  • $_marshalFormats protected
    array

    The DateTime formats allowed by marshal().

  • $_name protected
    string|null

    Identifier name for this type

  • $_useLocaleMarshal protected
    bool

    Whether marshal() should use locale-aware parser with _localeMarshalFormat.

  • $dbTimezone protected
    \DateTimeZone|null

    Database time zone.

  • $defaultTimezone protected
    \DateTimeZone

    Default time zone.

  • bool

    Whether database time zone is kept when converting

  • $setToDateStart protected
    bool

    Whether or not we want to override the time of the converted Time objects so it points to the start of the day.

Method Summary

  • __construct() public

    Constructor

  • _parseLocaleValue() protected

    Converts a string into a DateTime object after parsing it using the locale aware parser with the format set by setLocaleFormat().

  • _parseValue() protected

    Converts a string into a DateTime object after parsing it using the formats in _marshalFormats.

  • _setClassName() protected

    Set the classname to use when building objects.

  • getBaseType() public

    Returns the base type name that this class is inheriting.

  • getDateTimeClassName() public

    Get the classname used for building objects.

  • getName() public

    Returns type identifier name for this object.

  • manyToPHP() public

    Returns an array of the values converted to the PHP representation of this type.

  • marshal() public

    Convert request data into a datetime object.

  • newId() public

    Generate a new primary key value for a given type.

  • setDatabaseTimezone() public

    Set database timezone.

  • setKeepDatabaseTimezone() public

    Set whether DateTime object created from database string is converted to default time zone.

  • setLocaleFormat() public

    Sets the locale-aware format used by marshal() when parsing strings.

  • setTimezone() public

    Alias for setDatabaseTimezone().

  • toDatabase() public

    Convert DateTime instance into strings.

  • toPHP() public

    Casts given value from a database type to a PHP equivalent.

  • toStatement() public

    Casts given value to Statement equivalent

  • useImmutable() public

    Change the preferred class name to the FrozenTime implementation.

  • useLocaleParser() public

    Sets whether or not to parse strings passed to marshal() using the locale-aware format set by setLocaleFormat().

  • useMutable() public

    Change the preferred class name to the mutable Time implementation.

Method Detail

__construct() public

__construct(?string $name)

Constructor

Parameters

string|null $name optional

The name identifying this type

_parseLocaleValue() protected

_parseLocaleValue(string $value)

Converts a string into a DateTime object after parsing it using the locale aware parser with the format set by setLocaleFormat().

Parameters

string $value

The value to parse and convert to an object.

Returns

\Cake\I18n\I18nDateTimeInterface|null

_parseValue() protected

_parseValue(string $value)

Converts a string into a DateTime object after parsing it using the formats in _marshalFormats.

Parameters

string $value

The value to parse and convert to an object.

Returns

\DateTimeInterface|null

_setClassName() protected

_setClassName(string $class, string $fallback)

Set the classname to use when building objects.

Parameters

string $class

The classname to use.

string $fallback

The classname to use when the preferred class does not exist.

getBaseType() public

getBaseType()

Returns the base type name that this class is inheriting.

This is useful when extending base type for adding extra functionality, but still want the rest of the framework to use the same assumptions it would do about the base type it inherits from.

Returns

string|null

The base type name that this class is inheriting.

getDateTimeClassName() public

getDateTimeClassName()

Get the classname used for building objects.

Returns

string

getName() public

getName()

Returns type identifier name for this object.

Returns

string|null

The type identifier name for this object.

manyToPHP() public

manyToPHP(array $values, array $fields, \Cake\Database\DriverInterface $driver)

Returns an array of the values converted to the PHP representation of this type.

Parameters

array $values

The original array of values containing the fields to be casted

string[] $fields

The field keys to cast

\Cake\Database\DriverInterface $driver

Object from which database preferences and configuration will be extracted.

Returns

array

marshal() public

marshal(mixed $value)

Convert request data into a datetime object.

Parameters

mixed $value

Request data

Returns

\DateTimeInterface|null

newId() public

newId()

Generate a new primary key value for a given type.

This method can be used by types to create new primary key values when entities are inserted.

Returns

mixed

A new primary key value.

See Also

\Cake\Database\Type\UuidType

setDatabaseTimezone() public

setDatabaseTimezone(mixed $timezone)

Set database timezone.

This is the time zone used when converting database strings to DateTime instances and converting DateTime instances to database strings.

Parameters

string|\DateTimeZone|null $timezone

Database timezone.

Returns

$this

See Also

\Cake\Database\Type\DateTimeType::setKeepDatabaseTimezone

setKeepDatabaseTimezone() public

setKeepDatabaseTimezone(bool $keep)

Set whether DateTime object created from database string is converted to default time zone.

If your database date times are in a specific time zone that you want to keep in the DateTime instance then set this to true.

When false, datetime timezones are converted to default time zone. This is default behavior.

Parameters

bool $keep

If true, database time zone is kept when converting to DateTime instances.

Returns

$this

setLocaleFormat() public

setLocaleFormat(mixed $format)

Sets the locale-aware format used by marshal() when parsing strings.

See Cake\I18n\Time::parseDateTime() for accepted formats.

Parameters

string|array $format

The locale-aware format

Returns

$this

See Also

\Cake\I18n\Time::parseDateTime()

setTimezone() public

setTimezone(mixed $timezone)

Alias for setDatabaseTimezone().

Parameters

string|\DateTimeZone|null $timezone

Database timezone.

Returns

$this

toDatabase() public

toDatabase(mixed $value, \Cake\Database\DriverInterface $driver)

Convert DateTime instance into strings.

Parameters

mixed $value

The value to convert.

\Cake\Database\DriverInterface $driver

The driver instance to convert with.

Returns

string|null

toPHP() public

toPHP(mixed $value, \Cake\Database\DriverInterface $driver)

Casts given value from a database type to a PHP equivalent.

Parameters

mixed $value

Value to be converted to PHP equivalent

\Cake\Database\DriverInterface $driver

Object from which database preferences and configuration will be extracted

Returns

\DateTimeInterface|null

toStatement() public

toStatement(mixed $value, \Cake\Database\DriverInterface $driver)

Casts given value to Statement equivalent

Parameters

mixed $value

value to be converted to PDO statement

\Cake\Database\DriverInterface $driver

object from which database preferences and configuration will be extracted

Returns

mixed

useImmutable() public

useImmutable()

Change the preferred class name to the FrozenTime implementation.

Returns

$this

useLocaleParser() public

useLocaleParser(bool $enable)

Sets whether or not to parse strings passed to marshal() using the locale-aware format set by setLocaleFormat().

Parameters

bool $enable optional

Whether or not to enable

Returns

$this

useMutable() public

useMutable()

Change the preferred class name to the mutable Time implementation.

Returns

$this

Property Detail

$_className protected

The classname to use when creating objects.

Type

string

$_format protected

The DateTime format used when converting to string.

Type

string

$_localeMarshalFormat protected

The locale-aware format marshal() uses when _useLocaleParser is true.

See Cake\I18n\Time::parseDateTime() for accepted formats.

Type

string|array|int

$_marshalFormats protected

The DateTime formats allowed by marshal().

Type

array

$_name protected

Identifier name for this type

Type

string|null

$_useLocaleMarshal protected

Whether marshal() should use locale-aware parser with _localeMarshalFormat.

Type

bool

$dbTimezone protected

Database time zone.

Type

\DateTimeZone|null

$defaultTimezone protected

Default time zone.

Type

\DateTimeZone

$keepDatabaseTimezone protected

Whether database time zone is kept when converting

Type

bool

$setToDateStart protected

Whether or not we want to override the time of the converted Time objects so it points to the start of the day.

This is primarily to avoid subclasses needing to re-implement the same functionality.

Type

bool

© 2005–present The Cake Software Foundation, Inc.
Licensed under the MIT License.
CakePHP is a registered trademark of Cake Software Foundation, Inc.
We are not endorsed by or affiliated with CakePHP.
https://api.cakephp.org/4.1/class-Cake.Database.Type.DateTimeType.html