Fr3nch13/CakePHP Utilities

DifferenceTrait

Provides methods for getting differences between datetime objects.

Expects that the implementing class implements:

  • static::now()
  • static::instance()
  • copy()

Table of Contents

Properties

$diffFormatter  : DifferenceFormatterInterface|null
Instance of the diff formatting object.

Methods

diffFiltered()  : int
Get the difference by the given interval using a filter callable
diffForHumans()  : string
Get the difference in a human readable format.
diffFormatter()  : DifferenceFormatterInterface
Get the difference formatter instance or overwrite the current one.
diffInDays()  : int
Get the difference in days
diffInDaysFiltered()  : int
Get the difference in days using a filter callable
diffInHours()  : int
Get the difference in hours
diffInHoursFiltered()  : int
Get the difference in hours using a filter callable
diffInMinutes()  : int
Get the difference in minutes
diffInMonths()  : int
Get the difference in months
diffInMonthsIgnoreTimezone()  : int
Get the difference in months ignoring the timezone. This means the months are calculated in the specified timezone without converting to UTC first. This prevents the day from changing which can change the month.
diffInSeconds()  : int
Get the difference in seconds
diffInWeekdays()  : int
Get the difference in weekdays
diffInWeekendDays()  : int
Get the difference in weekend days using a filter
diffInWeeks()  : int
Get the difference in weeks
diffInYears()  : int
Get the difference in years
fromNow()  : DateInterval|bool
Convenience method for getting the remaining time from a given time.
secondsSinceMidnight()  : int
The number of seconds since midnight.
secondsUntilEndOfDay()  : int
The number of seconds until 23:59:59.

Properties

Methods

diffFiltered()

Get the difference by the given interval using a filter callable

public diffFiltered(ChronosInterval|DateInterval $ci, callable $callback[, ChronosInterface|null $dt = null ][, bool $abs = true ]) : int
Parameters
$ci : ChronosInterval|DateInterval

An interval to traverse by

$callback : callable

The callback to use for filtering.

$dt : ChronosInterface|null = null

The instance to difference from.

$abs : bool = true

Get the absolute of the difference

Return values
int

diffForHumans()

Get the difference in a human readable format.

public diffForHumans([ChronosInterface|null $other = null ][, bool $absolute = false ]) : string

When comparing a value in the past to default now: 1 hour ago 5 months ago

When comparing a value in the future to default now: 1 hour from now 5 months from now

When comparing a value in the past to another value: 1 hour before 5 months before

When comparing a value in the future to another value: 1 hour after 5 months after

Parameters
$other : ChronosInterface|null = null

The datetime to compare with.

$absolute : bool = false

removes time difference modifiers ago, after, etc

Return values
string

diffInDays()

Get the difference in days

public diffInDays([ChronosInterface|null $dt = null ][, bool $abs = true ]) : int
Parameters
$dt : ChronosInterface|null = null

The instance to difference from.

$abs : bool = true

Get the absolute of the difference

Return values
int

diffInDaysFiltered()

Get the difference in days using a filter callable

public diffInDaysFiltered(callable $callback[, ChronosInterface|null $dt = null ][, bool $abs = true ]) : int
Parameters
$callback : callable

The callback to use for filtering.

$dt : ChronosInterface|null = null

The instance to difference from.

$abs : bool = true

Get the absolute of the difference

Return values
int

diffInHours()

Get the difference in hours

public diffInHours([ChronosInterface|null $dt = null ][, bool $abs = true ]) : int
Parameters
$dt : ChronosInterface|null = null

The instance to difference from.

$abs : bool = true

Get the absolute of the difference

Return values
int

diffInHoursFiltered()

Get the difference in hours using a filter callable

public diffInHoursFiltered(callable $callback[, ChronosInterface|null $dt = null ][, bool $abs = true ]) : int
Parameters
$callback : callable

The callback to use for filtering.

$dt : ChronosInterface|null = null

The instance to difference from.

$abs : bool = true

Get the absolute of the difference

Return values
int

diffInMinutes()

Get the difference in minutes

public diffInMinutes([ChronosInterface|null $dt = null ][, bool $abs = true ]) : int
Parameters
$dt : ChronosInterface|null = null

The instance to difference from.

$abs : bool = true

Get the absolute of the difference

Return values
int

diffInMonths()

Get the difference in months

public diffInMonths([ChronosInterface|null $dt = null ][, bool $abs = true ]) : int
Parameters
$dt : ChronosInterface|null = null

The instance to difference from.

$abs : bool = true

Get the absolute of the difference

Return values
int

diffInMonthsIgnoreTimezone()

Get the difference in months ignoring the timezone. This means the months are calculated in the specified timezone without converting to UTC first. This prevents the day from changing which can change the month.

public diffInMonthsIgnoreTimezone([ChronosInterface|null $dt = null ][, bool $abs = true ]) : int

For example, if comparing 2019-06-01 Asia/Tokyo and 2019-10-01 Asia/Tokyo, the result would be 4 months instead of 3 when using normal DateTime::diff().

Parameters
$dt : ChronosInterface|null = null

The instance to difference from.

$abs : bool = true

Get the absolute of the difference

Return values
int

diffInSeconds()

Get the difference in seconds

public diffInSeconds([ChronosInterface|null $dt = null ][, bool $abs = true ]) : int
Parameters
$dt : ChronosInterface|null = null

The instance to difference from.

$abs : bool = true

Get the absolute of the difference

Return values
int

diffInWeekdays()

Get the difference in weekdays

public diffInWeekdays([ChronosInterface|null $dt = null ][, bool $abs = true ]) : int
Parameters
$dt : ChronosInterface|null = null

The instance to difference from.

$abs : bool = true

Get the absolute of the difference

Return values
int

diffInWeekendDays()

Get the difference in weekend days using a filter

public diffInWeekendDays([ChronosInterface|null $dt = null ][, bool $abs = true ]) : int
Parameters
$dt : ChronosInterface|null = null

The instance to difference from.

$abs : bool = true

Get the absolute of the difference

Return values
int

diffInWeeks()

Get the difference in weeks

public diffInWeeks([ChronosInterface|null $dt = null ][, bool $abs = true ]) : int
Parameters
$dt : ChronosInterface|null = null

The instance to difference from.

$abs : bool = true

Get the absolute of the difference

Return values
int

diffInYears()

Get the difference in years

public diffInYears([ChronosInterface|null $dt = null ][, bool $abs = true ]) : int
Parameters
$dt : ChronosInterface|null = null

The instance to difference from.

$abs : bool = true

Get the absolute of the difference

Return values
int

fromNow()

Convenience method for getting the remaining time from a given time.

public static fromNow(DateTime|DateTimeImmutable $datetime) : DateInterval|bool
Parameters
$datetime : DateTime|DateTimeImmutable

The date to get the remaining time from.

Return values
DateInterval|bool

The DateInterval object representing the difference between the two dates or FALSE on failure.

secondsSinceMidnight()

The number of seconds since midnight.

public secondsSinceMidnight() : int
Return values
int

secondsUntilEndOfDay()

The number of seconds until 23:59:59.

public secondsUntilEndOfDay() : int
Return values
int

        
On this page

Search results