Fr3nch13/CakePHP Utilities

FrozenTimeTrait

A trait for freezing the time aspect of a DateTime.

Used in making calendar date objects, both mutable and immutable.

Table of Contents

Methods

add()  : static
Add an Interval to a Date
modify()  : static
Overloaded to ignore time changes.
setTime()  : static
Modify the time on the Date.
setTimestamp()  : static
Set the timestamp value and get a new object back.
setTimezone()  : $this
No-op method.
sub()  : static
Subtract an Interval from a Date.
timezone()  : $this
No-op method.
tz()  : $this
No-op method.
stripRelativeTime()  : string
Remove time components from strtotime relative strings.
stripTime()  : string
Removes the time components from an input string.

Methods

add()

Add an Interval to a Date

public add(DateInterval $interval) : static

Any changes to the time will be ignored and reset to 00:00:00

Parameters
$interval : DateInterval

The interval to modify this date by.

Attributes
#[ReturnTypeWillChange]
Return values
static

A modified Date instance

modify()

Overloaded to ignore time changes.

public modify(string $relative) : static

Changing any aspect of the time will be ignored, and the resulting object will have its time frozen to 00:00:00.

Parameters
$relative : string

The relative change to make.

Attributes
#[ReturnTypeWillChange]
Return values
static

A new date with the applied date changes.

setTime()

Modify the time on the Date.

public setTime(int $hours, int $minutes[, int $seconds = null ][, int $microseconds = null ]) : static

This method ignores all inputs and forces all inputs to 0.

Parameters
$hours : int

The hours to set (ignored)

$minutes : int

The minutes to set (ignored)

$seconds : int = null

The seconds to set (ignored)

$microseconds : int = null

The microseconds to set (ignored)

Attributes
#[ReturnTypeWillChange]
Return values
static

A modified Date instance.

setTimestamp()

Set the timestamp value and get a new object back.

public setTimestamp(int $value) : static

This method will discard the time aspects of the timestamp and only apply the date portions

Parameters
$value : int

The timestamp value to set.

Attributes
#[ReturnTypeWillChange]
Return values
static

setTimezone()

No-op method.

public setTimezone(DateTimeZone|string $value) : $this

Timezones have no effect on calendar dates.

Parameters
$value : DateTimeZone|string

The DateTimeZone object or timezone name to use.

Attributes
#[ReturnTypeWillChange]
Return values
$this

sub()

Subtract an Interval from a Date.

public sub(DateInterval $interval) : static

Any changes to the time will be ignored and reset to 00:00:00

Parameters
$interval : DateInterval

The interval to modify this date by.

Attributes
#[ReturnTypeWillChange]
Return values
static

A modified Date instance

timezone()

No-op method.

public timezone(DateTimeZone|string $value) : $this

Timezones have no effect on calendar dates.

Parameters
$value : DateTimeZone|string

The DateTimeZone object or timezone name to use.

Return values
$this

tz()

No-op method.

public tz(DateTimeZone|string $value) : $this

Timezones have no effect on calendar dates.

Parameters
$value : DateTimeZone|string

The DateTimeZone object or timezone name to use.

Return values
$this

stripRelativeTime()

Remove time components from strtotime relative strings.

protected stripRelativeTime(string $time) : string
Parameters
$time : string

The input expression

Return values
string

The output expression with no time modifiers.

stripTime()

Removes the time components from an input string.

protected stripTime(DateTime|DateTimeImmutable|string|int|null $time, DateTimeZone|null $tz) : string

Used to ensure constructed objects always lack time.

Parameters
$time : DateTime|DateTimeImmutable|string|int|null

The input time. Integer values will be assumed to be in UTC. The 'now' and '' values will use the current local time.

$tz : DateTimeZone|null

The timezone in which the date is taken

Return values
string

The date component of $time.


        
On this page

Search results