DateTimeHelper
- Type
- Class
- Namespace
- craft\helpers
- Inherits
- craft\helpers\DateTimeHelper
- Since
- 3.0.0
Class DateTimeHelper
View source (opens new window)
# Public Methods
Method | Description |
---|---|
currentTimeStamp() | |
currentUTCDateTime() | |
humanDurationFromInterval() | Returns the interval in a human-friendly string. |
intervalToSeconds() | Returns the number of seconds that a given DateInterval object spans. |
isInThePast() | Returns true if the specified date was in the past, otherwise false. |
isIso8601() | Determines whether the given value is an ISO-8601-formatted date, as formatted by either DateTime::ATOM (opens new window) or DateTime::ISO8601 (opens new window) (with or without the colon between the hours and minutes of the timezone). |
isThisMonth() | Returns true if given date is in this month |
isThisWeek() | Returns true if given date is in this week |
isThisYear() | Returns true if given date is in this year |
isToday() | Returns true if given date is today. |
isValidIntervalString() | Returns true if interval string is a valid interval. |
isValidTimeStamp() | |
isWithinLast() | Returns true if specified datetime was within the interval specified, else false. |
isYesterday() | Returns true if given date was yesterday |
normalizeTimeZone() | Normalizes a timezone string to a PHP timezone identifier. |
secondsToHumanTimeDuration() | |
secondsToInterval() | Creates a DateInterval object based on a given number of seconds. |
timeZoneAbbreviation() | Returns the timezone abbreviation for a given timezone name. |
timeZoneOffset() | Returns a given timezone’s offset from UTC (e.g. '+10:00' or '-06:00'). |
toDateTime() | Converts a value into a DateTime object. |
toIso8601() | Converts a date to an ISO-8601 string. |
translateDate() | Translates the words in a formatted date string to the application’s language. |
# currentTimeStamp()
View source (opens new window)
Returns
# currentUTCDateTime()
View source (opens new window)
Returns
# humanDurationFromInterval()
Returns the interval in a human-friendly string.
View source (opens new window)
Arguments
$dateInterval
(DateInterval (opens new window))$showSeconds
(boolean (opens new window))
Returns
# intervalToSeconds()
Returns the number of seconds that a given DateInterval object spans.
View source (opens new window)
Arguments
$dateInterval
(DateInterval (opens new window))
Returns
# isInThePast()
Returns true if the specified date was in the past, otherwise false.
View source (opens new window)
Arguments
$date
(mixed
) – The timestamp to check
Returns
boolean (opens new window) – True if the specified date was in the past, false otherwise.
# isIso8601()
Determines whether the given value is an ISO-8601-formatted date, as formatted by either DateTime::ATOM (opens new window) or DateTime::ISO8601 (opens new window) (with or without the colon between the hours and minutes of the timezone).
View source (opens new window)
Arguments
$value
(mixed
) – The timestamp to check
Returns
boolean (opens new window) – Whether the value is an ISO-8601 date string
# isThisMonth()
Returns true if given date is in this month
View source (opens new window)
Arguments
$date
(mixed
) – The timestamp to check
Returns
boolean (opens new window) – True if date is in this month, false otherwise.
# isThisWeek()
Returns true if given date is in this week
View source (opens new window)
Arguments
$date
(mixed
) – The timestamp to check
Returns
boolean (opens new window) – True if date is in this week, false otherwise.
# isThisYear()
Returns true if given date is in this year
View source (opens new window)
Arguments
$date
(mixed
) – The timestamp to check
Returns
boolean (opens new window) – True if date is in this year, false otherwise.
# isToday()
Returns true if given date is today.
View source (opens new window)
Arguments
$date
(mixed
) – The timestamp to check
Returns
boolean (opens new window) – True if date is today, false otherwise.
# isValidIntervalString()
Returns true if interval string is a valid interval.
View source (opens new window)
Arguments
$intervalString
(string (opens new window))
Returns
# isValidTimeStamp()
View source (opens new window)
Arguments
$timestamp
(string (opens new window), integer (opens new window))
Returns
# isWithinLast()
Returns true if specified datetime was within the interval specified, else false.
View source (opens new window)
Arguments
$date
(mixed
) – The timestamp to check$timeInterval
(mixed
) – The numeric value with space then time type. Example of valid types: '6 hours', '2 days', '1 minute'.
Returns
boolean (opens new window) – Whether the $dateString was within the specified $timeInterval.
Throws
# isYesterday()
Returns true if given date was yesterday
View source (opens new window)
Arguments
$date
(mixed
) – The timestamp to check
Returns
boolean (opens new window) – True if date was yesterday, false otherwise.
# normalizeTimeZone()
Normalizes a timezone string to a PHP timezone identifier.
Supports the following formats:
- Time zone abbreviation (EST, MDT)
- Difference to Greenwich time (GMT) in hours, with/without a colon between the hours and minutes (+0200, -0200, +02:00, -02:00)
- A PHP timezone identifier (UTC, GMT, Atlantic/Azores)
View source (opens new window)
Arguments
$timeZone
(string (opens new window)) – The timezone to be normalized
Returns
string (opens new window), false (opens new window) – The PHP timezone identifier, or false
if it could not be determined
# secondsToHumanTimeDuration()
View source (opens new window)
Arguments
$seconds
(integer (opens new window)) – The number of seconds$showSeconds
(boolean (opens new window)) – Whether to output seconds or not
Returns
# secondsToInterval()
Creates a DateInterval object based on a given number of seconds.
View source (opens new window)
Arguments
$seconds
(integer (opens new window))
Returns
DateInterval (opens new window)
# timeZoneAbbreviation()
DEPRECATED
Deprecated in 3.7.64
Returns the timezone abbreviation for a given timezone name.
View source (opens new window)
Arguments
$timeZone
(string (opens new window))
Returns
# timeZoneOffset()
DEPRECATED
Deprecated in 3.7.64
Returns a given timezone’s offset from UTC (e.g. '+10:00' or '-06:00').
View source (opens new window)
Arguments
$timeZone
(string (opens new window))
Returns
# toDateTime()
Converts a value into a DateTime object.
$value
can be in the following formats:
- All W3C date and time formats (http://www.w3.org/TR/NOTE-datetime)
- MySQL DATE and DATETIME formats (http://dev.mysql.com/doc/refman/5.1/en/datetime.html)
- Relaxed versions of W3C and MySQL formats (single-digit months, days, and hours)
- Unix timestamps
now
- An array with at least one of these keys defined:
datetime
,date
, ortime
. Supported keys include:date
– a date string inYYYY-MM-DD
orYYYY-MM-DD HH:MM:SS.MU
formats or the current locale’s short date formattime
– a time string inHH:MM
orHH:MM:SS
(24-hour) format or the current locale’s short time formatdatetime
– A timestamp in any of the non-array formats supported by this methodtimezone
– A valid PHP timezone (opens new window). If set, this will override the assumed timezone per$assumeSystemTimeZone
.
View source (opens new window)
Arguments
$value
(string (opens new window), integer (opens new window), array (opens new window), null (opens new window)) – The value that should be converted to a DateTime object.$assumeSystemTimeZone
(boolean (opens new window)) – Whether it should be assumed that the value was set in the system timezone if the timezone was not specified. If this isfalse
, UTC will be assumed.$setToSystemTimeZone
(boolean (opens new window)) – Whether to set the resulting DateTime object to the system timezone.
Returns
DateTime (opens new window), false (opens new window) – The DateTime object, or false
if $object could not be converted to one
Throws
# toIso8601()
Converts a date to an ISO-8601 string.
View source (opens new window)
Arguments
$date
(mixed
) – The date, in any format that toDateTime() supports.
Returns
string (opens new window), false (opens new window) – The date formatted as an ISO-8601 string, or false
if $date was not a valid date
# translateDate()
DEPRECATED
Deprecated in 3.0.6. Use craft\i18n\Formatter::asDate() instead.
Translates the words in a formatted date string to the application’s language.
View source (opens new window)
Arguments
$str
(string (opens new window)) – The formatted date string$language
(string (opens new window), null (opens new window)) – The language code (e.g.en-US
,en
). If this is null, the current application language (opens new window) will be used.
Returns
string (opens new window) – The translated date string
# Constants
Constant | Description |
---|---|
SECONDS_DAY | |
SECONDS_HOUR | |
SECONDS_MINUTE | |
SECONDS_MONTH | |
SECONDS_YEAR |