Fr3nch13/CakePHP Utilities

TimeType extends DateTimeType
in package

Time type converter.

Use to convert time instances to strings & back.

Table of Contents

Properties

$_className  : string
The classname to use when creating objects.
$_format  : string
The DateTime format used when converting to string.
$_localeMarshalFormat  : array<string|int, mixed>|string|int
The locale-aware format `marshal()` uses when `_useLocaleParser` is true.
$_marshalFormats  : array<string|int, string>
The DateTime formats allowed by `marshal()`.
$_name  : string|null
Identifier name for this type
$_useLocaleMarshal  : bool
Whether `marshal()` should use locale-aware parser with `_localeMarshalFormat`.
$dbTimezone  : DateTimeZone|null
Database time zone.
$defaultTimezone  : DateTimeZone
Default time zone.
$keepDatabaseTimezone  : bool
Whether database time zone is kept when converting
$setToDateStart  : bool
Whether we want to override the time of the converted Time objects so it points to the start of the day.
$userTimezone  : DateTimeZone|null
User time zone.

Methods

__construct()  : mixed
Constructor
getBaseType()  : string|null
Returns the base type name that this class is inheriting.
getDateTimeClassName()  : string
Get the classname used for building objects.
getName()  : string|null
Returns type identifier name for this object.
manyToPHP()  : array<string, mixed>
Returns an array of the values converted to the PHP representation of this type.
marshal()  : DateTimeInterface|null
Convert request data into a datetime object.
newId()  : mixed
Generate a new primary key value for a given type.
setDatabaseTimezone()  : $this
Set database timezone.
setKeepDatabaseTimezone()  : $this
Set whether DateTime object created from database string is converted to default time zone.
setLocaleFormat()  : $this
Sets the locale-aware format used by `marshal()` when parsing strings.
setTimezone()  : $this
Alias for `setDatabaseTimezone()`.
setUserTimezone()  : $this
Set user timezone.
toDatabase()  : string|null
Convert DateTime instance into strings.
toPHP()  : DateTimeInterface|null
Casts given value from a database type to a PHP equivalent.
toStatement()  : mixed
Casts given value to Statement equivalent
useImmutable()  : $this
Change the preferred class name to the FrozenTime implementation.
useLocaleParser()  : $this
Sets whether to parse strings passed to `marshal()` using the locale-aware format set by `setLocaleFormat()`.
useMutable()  : $this
Change the preferred class name to the mutable Time implementation.
_parseLocaleValue()  : I18nDateTimeInterface|null
Converts a string into a DateTime object after parsing it using the locale aware parser with the format set by `setLocaleFormat()`.
_parseValue()  : DateTimeInterface|null
Converts a string into a DateTime object after parsing it using the formats in `_marshalFormats`.
_setClassName()  : void
Set the classname to use when building objects.

Properties

$_className

The classname to use when creating objects.

protected string $_className
Tags
psalm-var

class-string<\DateTime>|class-string<\DateTimeImmutable>

$_format

The DateTime format used when converting to string.

protected string $_format = 'H:i:s'
Tags
inheritDoc

$_localeMarshalFormat

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

protected array<string|int, mixed>|string|int $_localeMarshalFormat

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

$_marshalFormats

The DateTime formats allowed by `marshal()`.

protected array<string|int, string> $_marshalFormats = ['H:i:s', 'H:i']
Tags
inheritDoc

$_name

Identifier name for this type

protected string|null $_name

$_useLocaleMarshal

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

protected bool $_useLocaleMarshal = false

$dbTimezone

Database time zone.

protected DateTimeZone|null $dbTimezone

$defaultTimezone

Default time zone.

protected DateTimeZone $defaultTimezone

$keepDatabaseTimezone

Whether database time zone is kept when converting

protected bool $keepDatabaseTimezone = false

$setToDateStart

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

protected bool $setToDateStart = false

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

$userTimezone

User time zone.

protected DateTimeZone|null $userTimezone

Methods

__construct()

Constructor

public __construct([string|null $name = null ]) : mixed
Parameters
$name : string|null = null

The name identifying this type

getBaseType()

Returns the base type name that this class is inheriting.

public getBaseType() : string|null
Tags
inheritDoc
Return values
string|null

The base type name that this class is inheriting.

getDateTimeClassName()

Get the classname used for building objects.

public getDateTimeClassName() : string
Tags
psalm-return

class-string<\DateTime>|class-string<\DateTimeImmutable>

Return values
string

getName()

Returns type identifier name for this object.

public getName() : string|null
Tags
inheritDoc
Return values
string|null

The type identifier name for this object.

manyToPHP()

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

public manyToPHP(array<string|int, mixed> $values, array<string|int, mixed> $fields, DriverInterface $driver) : array<string, mixed>
Parameters
$values : array<string|int, mixed>

The original array of values containing the fields to be casted

$fields : array<string|int, mixed>

The field keys to cast

$driver : DriverInterface

Object from which database preferences and configuration will be extracted.

Tags
inheritDoc
Return values
array<string, mixed>

marshal()

Convert request data into a datetime object.

public marshal(mixed $value) : DateTimeInterface|null
Parameters
$value : mixed

Request data

Return values
DateTimeInterface|null

newId()

Generate a new primary key value for a given type.

public newId() : mixed
Tags
inheritDoc
Return values
mixed

A new primary key value.

setDatabaseTimezone()

Set database timezone.

public setDatabaseTimezone(DateTimeZone|string|null $timezone) : $this

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

Parameters
$timezone : DateTimeZone|string|null

Database timezone.

Tags
see
DateTimeType::setKeepDatabaseTimezone
Return values
$this

setKeepDatabaseTimezone()

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

public setKeepDatabaseTimezone(bool $keep) : $this

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
$keep : bool

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

Return values
$this

setLocaleFormat()

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

public setLocaleFormat(array<string|int, mixed>|string $format) : $this

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

Parameters
$format : array<string|int, mixed>|string

The locale-aware format

Tags
see
Time::parseDateTime()
Return values
$this

setTimezone()

Alias for `setDatabaseTimezone()`.

public setTimezone(DateTimeZone|string|null $timezone) : $this

Use instead.

Parameters
$timezone : DateTimeZone|string|null

Database timezone.

Return values
$this

setUserTimezone()

Set user timezone.

public setUserTimezone(DateTimeZone|string|null $timezone) : $this

This is the time zone used when marshalling strings to DateTime instances.

Parameters
$timezone : DateTimeZone|string|null

User timezone.

Return values
$this

toDatabase()

Convert DateTime instance into strings.

public toDatabase(mixed $value, DriverInterface $driver) : string|null
Parameters
$value : mixed

The value to convert.

$driver : DriverInterface

The driver instance to convert with.

Return values
string|null

toPHP()

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

public toPHP(mixed $value, DriverInterface $driver) : DateTimeInterface|null
Parameters
$value : mixed

Value to be converted to PHP equivalent

$driver : DriverInterface

Object from which database preferences and configuration will be extracted

Return values
DateTimeInterface|null

toStatement()

Casts given value to Statement equivalent

public toStatement(mixed $value, DriverInterface $driver) : mixed
Parameters
$value : mixed

value to be converted to PDO statement

$driver : DriverInterface

object from which database preferences and configuration will be extracted

useImmutable()

Change the preferred class name to the FrozenTime implementation.

public useImmutable() : $this

This method is no longer needed as using immutable datetime class is the default behavior.

Return values
$this

useLocaleParser()

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

public useLocaleParser([bool $enable = true ]) : $this
Parameters
$enable : bool = true

Whether to enable

Return values
$this

useMutable()

Change the preferred class name to the mutable Time implementation.

public useMutable() : $this

Using mutable datetime objects is deprecated.

Return values
$this

_parseLocaleValue()

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

protected _parseLocaleValue(string $value) : I18nDateTimeInterface|null
Parameters
$value : string

The value to parse and convert to an object.

Tags
inheritDoc
Return values
I18nDateTimeInterface|null

_parseValue()

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

protected _parseValue(string $value) : DateTimeInterface|null
Parameters
$value : string

The value to parse and convert to an object.

Return values
DateTimeInterface|null

_setClassName()

Set the classname to use when building objects.

protected _setClassName(string $class, string $fallback) : void
Parameters
$class : string

The classname to use.

$fallback : string

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

Tags
psalm-param

class-string<\DateTime>|class-string<\DateTimeImmutable> $class

psalm-param

class-string<\DateTime>|class-string<\DateTimeImmutable> $fallback


        
On this page

Search results