TimeHelper
extends Helper
in package
uses
StringTemplateTrait
Time Helper class for easy use of time data.
Manipulation of time data.
Tags
Table of Contents
Properties
- $_config : array<string, mixed>
- Runtime config
- $_configInitialized : bool
- Whether the config property has already been configured with defaults
- $_defaultConfig : array<string, mixed>
- Config options
- $_helperMap : array<string, array<string|int, mixed>>
- A helper lookup table used to lazy load helper objects.
- $_templater : StringTemplate|null
- StringTemplate instance.
- $_View : View
- The View instance this helper is attached to
- $helpers : array<string|int, mixed>
- List of helpers used by this helper
Methods
- __call() : mixed|void
- Provide non fatal errors on missing method calls.
- __construct() : mixed
- Default Constructor
- __debugInfo() : array<string, mixed>
- Returns an array that can be used to describe the internal state of this object.
- __get() : Helper|null|void
- Lazy loads helpers.
- addClass() : array<string, mixed>
- Adds the given class to the element options
- configShallow() : $this
- Merge provided config with existing config. Unlike `config()` which does a recursive merge for nested keys, this method does a simple merge.
- format() : string|int|false
- Returns a formatted date string, given either a Time instance, UNIX timestamp or a valid strtotime() date string.
- formatTemplate() : string
- Formats a template string with $data
- fromString() : FrozenTime
- Returns a UNIX timestamp, given either a UNIX timestamp or a valid strtotime() date string.
- getConfig() : mixed
- Returns the config.
- getConfigOrFail() : mixed
- Returns the config for this specific key.
- getTemplates() : array<string|int, mixed>|string
- Gets templates to use or a specific template.
- getView() : View
- Get the view instance this helper is bound to.
- gmt() : string
- Returns gmt as a UNIX timestamp.
- i18nFormat() : string|int|false
- Returns a formatted date string, given either a Datetime instance, UNIX timestamp or a valid strtotime() date string.
- implementedEvents() : array<string, mixed>
- Event listeners.
- initialize() : void
- Constructor hook method.
- isFuture() : bool
- Returns true, if the given datetime string is in the future.
- isPast() : bool
- Returns true, if the given datetime string is in the past.
- isThisMonth() : bool
- Returns true if given datetime string is within this month
- isThisWeek() : bool
- Returns true if given datetime string is within this week.
- isThisYear() : bool
- Returns true if given datetime string is within the current year.
- isToday() : bool
- Returns true, if the given datetime string is today.
- isTomorrow() : bool
- Returns true if given datetime string is tomorrow.
- isWithinNext() : bool
- Returns true if specified datetime is within the interval specified, else false.
- nice() : string
- Returns a nicely formatted date string for given Datetime string.
- setConfig() : $this
- Sets the config.
- setTemplates() : $this
- Sets templates to use.
- templater() : StringTemplate
- Returns the templater instance.
- timeAgoInWords() : string
- Formats a date into a phrase expressing the relative time.
- toAtom() : string
- Returns a date formatted for Atom RSS feeds.
- toQuarter() : array<string|int, string>|int
- Returns the quarter
- toRss() : string
- Formats date for RSS feeds
- toUnix() : string
- Returns a UNIX timestamp from a textual datetime description.
- wasWithinLast() : bool
- Returns true if specified datetime was within the interval specified, else false.
- wasYesterday() : bool
- Returns true if given datetime string was yesterday.
- _configDelete() : void
- Deletes a single config key.
- _configRead() : mixed
- Reads a config key.
- _configWrite() : void
- Writes a config key.
- _confirm() : string
- Returns a string to be used as onclick handler for confirm dialogs.
- _getTimezone() : DateTimeZone|string|null
- Get a timezone.
Properties
$_config
Runtime config
protected
array<string, mixed>
$_config
= []
$_configInitialized
Whether the config property has already been configured with defaults
protected
bool
$_configInitialized
= false
$_defaultConfig
Config options
protected
array<string, mixed>
$_defaultConfig
= ['outputTimezone' => null]
$_helperMap
A helper lookup table used to lazy load helper objects.
protected
array<string, array<string|int, mixed>>
$_helperMap
= []
$_templater
StringTemplate instance.
protected
StringTemplate|null
$_templater
$_View
The View instance this helper is attached to
protected
View
$_View
$helpers
List of helpers used by this helper
protected
array<string|int, mixed>
$helpers
= []
Methods
__call()
Provide non fatal errors on missing method calls.
public
__call(string $method, array<string|int, mixed> $params) : mixed|void
Parameters
- $method : string
-
Method to invoke
- $params : array<string|int, mixed>
-
Array of params for the method.
Return values
mixed|void__construct()
Default Constructor
public
__construct(View $view[, array<string, mixed> $config = [] ]) : mixed
Parameters
- $view : View
-
The View this helper is being attached to.
- $config : array<string, mixed> = []
-
Configuration settings for the helper.
__debugInfo()
Returns an array that can be used to describe the internal state of this object.
public
__debugInfo() : array<string, mixed>
Return values
array<string, mixed>__get()
Lazy loads helpers.
public
__get(string $name) : Helper|null|void
Parameters
- $name : string
-
Name of the property being accessed.
Return values
Helper|null|void —Helper instance if helper with provided name exists
addClass()
Adds the given class to the element options
public
addClass(array<string, mixed> $options, string $class[, string $key = 'class' ]) : array<string, mixed>
Parameters
- $options : array<string, mixed>
-
Array options/attributes to add a class to
- $class : string
-
The class name being added.
- $key : string = 'class'
-
the key to use for class. Defaults to
'class'
.
Return values
array<string, mixed> —Array of options with $key set.
configShallow()
Merge provided config with existing config. Unlike `config()` which does a recursive merge for nested keys, this method does a simple merge.
public
configShallow(array<string, mixed>|string $key[, mixed|null $value = null ]) : $this
Setting a specific value:
$this->configShallow('key', $value);
Setting a nested value:
$this->configShallow('some.nested.key', $value);
Updating multiple config settings at the same time:
$this->configShallow(['one' => 'value', 'another' => 'value']);
Parameters
- $key : array<string, mixed>|string
-
The key to set, or a complete array of configs.
- $value : mixed|null = null
-
The value to set.
Return values
$thisformat()
Returns a formatted date string, given either a Time instance, UNIX timestamp or a valid strtotime() date string.
public
format(DateTimeInterface|string|int|null $date[, string|int|null $format = null ][, string|false $invalid = false ][, DateTimeZone|string|null $timezone = null ]) : string|int|false
This method is an alias for TimeHelper::i18nFormat().
Parameters
- $date : DateTimeInterface|string|int|null
-
UNIX timestamp, strtotime() valid string or DateTime object (or a date format string).
- $format : string|int|null = null
-
date format string (or a UNIX timestamp,
strtotime()
valid string or DateTime object). - $invalid : string|false = false
-
Default value to display on invalid dates
- $timezone : DateTimeZone|string|null = null
-
User's timezone string or DateTimeZone object
Tags
Return values
string|int|false —Formatted and translated date string
formatTemplate()
Formats a template string with $data
public
formatTemplate(string $name, array<string, mixed> $data) : string
Parameters
- $name : string
-
The template name.
- $data : array<string, mixed>
-
The data to insert.
Return values
stringfromString()
Returns a UNIX timestamp, given either a UNIX timestamp or a valid strtotime() date string.
public
fromString(DateTimeInterface|string|int $dateString[, DateTimeZone|string|null $timezone = null ]) : FrozenTime
Parameters
- $dateString : DateTimeInterface|string|int
-
UNIX timestamp, strtotime() valid string or DateTime object
- $timezone : DateTimeZone|string|null = null
-
User's timezone string or DateTimeZone object
Return values
FrozenTimegetConfig()
Returns the config.
public
getConfig([string|null $key = null ][, mixed $default = null ]) : mixed
Usage
Reading the whole config:
$this->getConfig();
Reading a specific value:
$this->getConfig('key');
Reading a nested value:
$this->getConfig('some.nested.key');
Reading with default value:
$this->getConfig('some-key', 'default-value');
Parameters
- $key : string|null = null
-
The key to get or null for the whole config.
- $default : mixed = null
-
The return value when the key does not exist.
Return values
mixed —Configuration data at the named key or null if the key does not exist.
getConfigOrFail()
Returns the config for this specific key.
public
getConfigOrFail(string $key) : mixed
The config value for this key must exist, it can never be null.
Parameters
- $key : string
-
The key to get.
Tags
Return values
mixed —Configuration data at the named key
getTemplates()
Gets templates to use or a specific template.
public
getTemplates([string|null $template = null ]) : array<string|int, mixed>|string
Parameters
- $template : string|null = null
-
String for reading a specific template, null for all.
Return values
array<string|int, mixed>|stringgetView()
Get the view instance this helper is bound to.
public
getView() : View
Return values
View —The bound view instance.
gmt()
Returns gmt as a UNIX timestamp.
public
gmt([DateTimeInterface|string|int|null $string = null ]) : string
Parameters
- $string : DateTimeInterface|string|int|null = null
-
UNIX timestamp, strtotime() valid string or DateTime object
Tags
Return values
string —UNIX timestamp
i18nFormat()
Returns a formatted date string, given either a Datetime instance, UNIX timestamp or a valid strtotime() date string.
public
i18nFormat(DateTimeInterface|string|int|null $date[, string|int|null $format = null ][, string|false $invalid = false ][, DateTimeZone|string|null $timezone = null ]) : string|int|false
Parameters
- $date : DateTimeInterface|string|int|null
-
UNIX timestamp, strtotime() valid string or DateTime object
- $format : string|int|null = null
-
Intl compatible format string.
- $invalid : string|false = false
-
Default value to display on invalid dates
- $timezone : DateTimeZone|string|null = null
-
User's timezone string or DateTimeZone object
Tags
Return values
string|int|false —Formatted and translated date string or value for $invalid
on failure.
implementedEvents()
Event listeners.
public
implementedEvents() : array<string, mixed>
Return values
array<string, mixed>initialize()
Constructor hook method.
public
initialize(array<string, mixed> $config) : void
Implement this method to avoid having to overwrite the constructor and call parent.
Parameters
- $config : array<string, mixed>
-
The configuration settings provided to this helper.
isFuture()
Returns true, if the given datetime string is in the future.
public
isFuture(DateTimeInterface|string|int $dateString[, DateTimeZone|string|null $timezone = null ]) : bool
Parameters
- $dateString : DateTimeInterface|string|int
-
UNIX timestamp, strtotime() valid string or DateTime object
- $timezone : DateTimeZone|string|null = null
-
User's timezone string or DateTimeZone object
Return values
bool —True if the given datetime string lies in the future.
isPast()
Returns true, if the given datetime string is in the past.
public
isPast(DateTimeInterface|string|int $dateString[, DateTimeZone|string|null $timezone = null ]) : bool
Parameters
- $dateString : DateTimeInterface|string|int
-
UNIX timestamp, strtotime() valid string or DateTime object
- $timezone : DateTimeZone|string|null = null
-
User's timezone string or DateTimeZone object
Return values
bool —True if the given datetime string lies in the past.
isThisMonth()
Returns true if given datetime string is within this month
public
isThisMonth(DateTimeInterface|string|int $dateString[, DateTimeZone|string|null $timezone = null ]) : bool
Parameters
- $dateString : DateTimeInterface|string|int
-
UNIX timestamp, strtotime() valid string or DateTime object
- $timezone : DateTimeZone|string|null = null
-
User's timezone string or DateTimeZone object
Return values
bool —True if datetime string is within the current month
isThisWeek()
Returns true if given datetime string is within this week.
public
isThisWeek(DateTimeInterface|string|int $dateString[, DateTimeZone|string|null $timezone = null ]) : bool
Parameters
- $dateString : DateTimeInterface|string|int
-
UNIX timestamp, strtotime() valid string or DateTime object
- $timezone : DateTimeZone|string|null = null
-
User's timezone string or DateTimeZone object
Return values
bool —True if datetime string is within current week
isThisYear()
Returns true if given datetime string is within the current year.
public
isThisYear(DateTimeInterface|string|int $dateString[, DateTimeZone|string|null $timezone = null ]) : bool
Parameters
- $dateString : DateTimeInterface|string|int
-
UNIX timestamp, strtotime() valid string or DateTime object
- $timezone : DateTimeZone|string|null = null
-
User's timezone string or DateTimeZone object
Return values
bool —True if datetime string is within current year
isToday()
Returns true, if the given datetime string is today.
public
isToday(DateTimeInterface|string|int $dateString[, DateTimeZone|string|null $timezone = null ]) : bool
Parameters
- $dateString : DateTimeInterface|string|int
-
UNIX timestamp, strtotime() valid string or DateTime object
- $timezone : DateTimeZone|string|null = null
-
User's timezone string or DateTimeZone object
Return values
bool —True if the given datetime string is today.
isTomorrow()
Returns true if given datetime string is tomorrow.
public
isTomorrow(DateTimeInterface|string|int $dateString[, DateTimeZone|string|null $timezone = null ]) : bool
Parameters
- $dateString : DateTimeInterface|string|int
-
UNIX timestamp, strtotime() valid string or DateTime object
- $timezone : DateTimeZone|string|null = null
-
User's timezone string or DateTimeZone object
Return values
bool —True if datetime string was yesterday
isWithinNext()
Returns true if specified datetime is within the interval specified, else false.
public
isWithinNext(string $timeInterval, DateTimeInterface|string|int $dateString[, DateTimeZone|string|null $timezone = null ]) : bool
Parameters
- $timeInterval : string
-
the numeric value with space then time type. Example of valid types: 6 hours, 2 days, 1 minute.
- $dateString : DateTimeInterface|string|int
-
UNIX timestamp, strtotime() valid string or DateTime object
- $timezone : DateTimeZone|string|null = null
-
User's timezone string or DateTimeZone object
Tags
Return values
boolnice()
Returns a nicely formatted date string for given Datetime string.
public
nice([DateTimeInterface|string|int|null $dateString = null ][, DateTimeZone|string|null $timezone = null ][, string|null $locale = null ]) : string
Parameters
- $dateString : DateTimeInterface|string|int|null = null
-
UNIX timestamp, strtotime() valid string or DateTime object
- $timezone : DateTimeZone|string|null = null
-
User's timezone string or DateTimeZone object
- $locale : string|null = null
-
Locale string.
Return values
string —Formatted date string
setConfig()
Sets the config.
public
setConfig(array<string, mixed>|string $key[, mixed|null $value = null ][, bool $merge = true ]) : $this
Usage
Setting a specific value:
$this->setConfig('key', $value);
Setting a nested value:
$this->setConfig('some.nested.key', $value);
Updating multiple config settings at the same time:
$this->setConfig(['one' => 'value', 'another' => 'value']);
Parameters
- $key : array<string, mixed>|string
-
The key to set, or a complete array of configs.
- $value : mixed|null = null
-
The value to set.
- $merge : bool = true
-
Whether to recursively merge or overwrite existing config, defaults to true.
Tags
Return values
$thissetTemplates()
Sets templates to use.
public
setTemplates(array<string|int, string> $templates) : $this
Parameters
- $templates : array<string|int, string>
-
Templates to be added.
Return values
$thistemplater()
Returns the templater instance.
public
templater() : StringTemplate
Return values
StringTemplatetimeAgoInWords()
Formats a date into a phrase expressing the relative time.
public
timeAgoInWords(DateTimeInterface|string|int $dateTime[, array<string, mixed> $options = [] ]) : string
Additional options
-
element
- The element to wrap the formatted time in. Has a few additional options:-
tag
- The tag to use, defaults to 'span'. -
class
- The class name to use, defaults totime-ago-in-words
. -
title
- Defaults to the $dateTime input.
-
Parameters
- $dateTime : DateTimeInterface|string|int
-
UNIX timestamp, strtotime() valid string or DateTime object.
- $options : array<string, mixed> = []
-
Default format if timestamp is used in $dateString
Tags
Return values
string —Relative time string.
toAtom()
Returns a date formatted for Atom RSS feeds.
public
toAtom(DateTimeInterface|string|int $dateString[, DateTimeZone|string|null $timezone = null ]) : string
Parameters
- $dateString : DateTimeInterface|string|int
-
UNIX timestamp, strtotime() valid string or DateTime object
- $timezone : DateTimeZone|string|null = null
-
User's timezone string or DateTimeZone object
Tags
Return values
string —Formatted date string
toQuarter()
Returns the quarter
public
toQuarter(DateTimeInterface|string|int $dateString[, bool $range = false ]) : array<string|int, string>|int
Parameters
- $dateString : DateTimeInterface|string|int
-
UNIX timestamp, strtotime() valid string or DateTime object
- $range : bool = false
-
if true returns a range in Y-m-d format
Tags
Return values
array<string|int, string>|int —1, 2, 3, or 4 quarter of year or array if $range true
toRss()
Formats date for RSS feeds
public
toRss(DateTimeInterface|string|int $dateString[, DateTimeZone|string|null $timezone = null ]) : string
Parameters
- $dateString : DateTimeInterface|string|int
-
UNIX timestamp, strtotime() valid string or DateTime object
- $timezone : DateTimeZone|string|null = null
-
User's timezone string or DateTimeZone object
Return values
string —Formatted date string
toUnix()
Returns a UNIX timestamp from a textual datetime description.
public
toUnix(DateTimeInterface|string|int $dateString[, DateTimeZone|string|null $timezone = null ]) : string
Parameters
- $dateString : DateTimeInterface|string|int
-
UNIX timestamp, strtotime() valid string or DateTime object
- $timezone : DateTimeZone|string|null = null
-
User's timezone string or DateTimeZone object
Tags
Return values
string —UNIX timestamp
wasWithinLast()
Returns true if specified datetime was within the interval specified, else false.
public
wasWithinLast(string $timeInterval, DateTimeInterface|string|int $dateString[, DateTimeZone|string|null $timezone = null ]) : bool
Parameters
- $timeInterval : string
-
the numeric value with space then time type. Example of valid types: 6 hours, 2 days, 1 minute.
- $dateString : DateTimeInterface|string|int
-
UNIX timestamp, strtotime() valid string or DateTime object
- $timezone : DateTimeZone|string|null = null
-
User's timezone string or DateTimeZone object
Tags
Return values
boolwasYesterday()
Returns true if given datetime string was yesterday.
public
wasYesterday(DateTimeInterface|string|int $dateString[, DateTimeZone|string|null $timezone = null ]) : bool
Parameters
- $dateString : DateTimeInterface|string|int
-
UNIX timestamp, strtotime() valid string or DateTime object
- $timezone : DateTimeZone|string|null = null
-
User's timezone string or DateTimeZone object
Return values
bool —True if datetime string was yesterday
_configDelete()
Deletes a single config key.
protected
_configDelete(string $key) : void
Parameters
- $key : string
-
Key to delete.
Tags
_configRead()
Reads a config key.
protected
_configRead(string|null $key) : mixed
Parameters
- $key : string|null
-
Key to read.
_configWrite()
Writes a config key.
protected
_configWrite(array<string, mixed>|string $key, mixed $value[, string|bool $merge = false ]) : void
Parameters
- $key : array<string, mixed>|string
-
Key to write to.
- $value : mixed
-
Value to write.
- $merge : string|bool = false
-
True to merge recursively, 'shallow' for simple merge, false to overwrite, defaults to false.
Tags
_confirm()
Returns a string to be used as onclick handler for confirm dialogs.
protected
_confirm(string $okCode, string $cancelCode) : string
Parameters
- $okCode : string
-
Code to be executed after user chose 'OK'
- $cancelCode : string
-
Code to be executed after user chose 'Cancel'
Return values
string —"onclick" JS code
_getTimezone()
Get a timezone.
protected
_getTimezone(DateTimeZone|string|null $timezone) : DateTimeZone|string|null
Will use the provided timezone, or default output timezone if defined.
Parameters
- $timezone : DateTimeZone|string|null
-
The override timezone if applicable.
Return values
DateTimeZone|string|null —The chosen timezone or null.