Craft 3 Class Reference

Class craft\helpers\DateTimeHelper

Inheritance
craft\helpers\DateTimeHelper
Available since version
3.0
Source Code
https://github.com/craftcms/cms/blob/master/src/helpers/DateTimeHelper.php

Class DateTimeHelper

Public Methods
Method Description Defined By
currentTimeStamp() craft\helpers\DateTimeHelper
currentUTCDateTime() craft\helpers\DateTimeHelper
humanDurationFromInterval() Returns the interval in a human-friendly string. craft\helpers\DateTimeHelper
intervalToSeconds() Returns the number of seconds that a given DateInterval object spans. craft\helpers\DateTimeHelper
isInThePast() Returns true if the specified date was in the past, otherwise false. craft\helpers\DateTimeHelper
isIso8601() Determines whether the given value is an ISO-8601-formatted date, as formatted by either DateTime::ATOM or DateTime::ISO8601 (with or without the colon between the hours and minutes of the timezone). craft\helpers\DateTimeHelper
isThisMonth() Returns true if given date is in this month craft\helpers\DateTimeHelper
isThisWeek() Returns true if given date is in this week craft\helpers\DateTimeHelper
isThisYear() Returns true if given date is in this year craft\helpers\DateTimeHelper
isToday() Returns true if given date is today. craft\helpers\DateTimeHelper
isValidIntervalString() Returns true if interval string is a valid interval. craft\helpers\DateTimeHelper
isValidTimeStamp() craft\helpers\DateTimeHelper
isWithinLast() Returns true if specified datetime was within the interval specified, else false. craft\helpers\DateTimeHelper
isYesterday() Returns true if given date was yesterday craft\helpers\DateTimeHelper
normalizeTimeZone() Normalizes a time zone string to a PHP time zone identifier. craft\helpers\DateTimeHelper
secondsToHumanTimeDuration() craft\helpers\DateTimeHelper
secondsToInterval() Creates a DateInterval object based on a given number of seconds. craft\helpers\DateTimeHelper
timeZoneAbbreviation() Returns the timezone abbreviation for a given timezone name. craft\helpers\DateTimeHelper
timeZoneOffset() Returns a given timezone’s offset from UTC (e.g. '+10:00' or '-06:00'). craft\helpers\DateTimeHelper
toDateTime() Converts a value into a DateTime object. craft\helpers\DateTimeHelper
toIso8601() Converts a date to an ISO-8601 string. craft\helpers\DateTimeHelper
translateDate() Translates the words in a formatted date string to the application’s language. craft\helpers\DateTimeHelper
Constants
Constant Value Description Defined By
SECONDS_DAY 86400 Number of seconds in a day. craft\helpers\DateTimeHelper
SECONDS_HOUR 3600 Number of seconds in an hour. craft\helpers\DateTimeHelper
SECONDS_MINUTE 60 Number of seconds in a minute. craft\helpers\DateTimeHelper
SECONDS_MONTH 2629740 The number of seconds in a month. Based on a 30.4368 day month, with the product rounded. craft\helpers\DateTimeHelper
SECONDS_YEAR 31556874 The number of seconds in a year. Based on a 365.2416 day year, with the product rounded. craft\helpers\DateTimeHelper

Method Details

currentTimeStamp() public static method #

public static integer currentTimeStamp ( )

currentUTCDateTime() public static method #

public static DateTime currentUTCDateTime ( )

humanDurationFromInterval() public static method #

Returns the interval in a human-friendly string.

public static string humanDurationFromInterval ( DateInterval $dateInterval, \craft\helpers\bool $showSeconds true )
$dateInterval DateInterval
$showSeconds boolean

intervalToSeconds() public static method #

Returns the number of seconds that a given DateInterval object spans.

public static integer intervalToSeconds ( DateInterval $dateInterval )
$dateInterval DateInterval

isInThePast() public static method #

Returns true if the specified date was in the past, otherwise false.

public static boolean isInThePast ( $date )
$date mixed The timestamp to check
return boolean True if the specified date was in the past, false otherwise.

isIso8601() public static method #

Determines whether the given value is an ISO-8601-formatted date, as formatted by either DateTime::ATOM or DateTime::ISO8601 (with or without the colon between the hours and minutes of the timezone).

public static boolean isIso8601 ( $value )
$value mixed The timestamp to check
return boolean Whether the value is an ISO-8601 date string

isThisMonth() public static method #

Returns true if given date is in this month

public static boolean isThisMonth ( $date )
$date mixed The timestamp to check
return boolean True if date is in this month, false otherwise.

isThisWeek() public static method #

Returns true if given date is in this week

public static boolean isThisWeek ( $date )
$date mixed The timestamp to check
return boolean True if date is in this week, false otherwise.

isThisYear() public static method #

Returns true if given date is in this year

public static boolean isThisYear ( $date )
$date mixed The timestamp to check
return boolean True if date is in this year, false otherwise.

isToday() public static method #

Returns true if given date is today.

public static boolean isToday ( $date )
$date mixed The timestamp to check
return boolean True if date is today, false otherwise.

isValidIntervalString() public static method #

Returns true if interval string is a valid interval.

public static boolean isValidIntervalString ( \craft\helpers\string $intervalString )
$intervalString string

isValidTimeStamp() public static method #

public static boolean isValidTimeStamp ( $timestamp )
$timestamp string, integer

isWithinLast() public static method #

Returns true if specified datetime was within the interval specified, else false.

public static boolean isWithinLast ( $date, $timeInterval )
$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'.
return boolean Whether the $dateString was within the specified $timeInterval.

isYesterday() public static method #

Returns true if given date was yesterday

public static boolean isYesterday ( $date )
$date mixed The timestamp to check
return boolean True if date was yesterday, false otherwise.

normalizeTimeZone() public static method #

Normalizes a time zone string to a PHP time zone identifier.

Supports the following formats:

public static string, false normalizeTimeZone ( \craft\helpers\string $timeZone )
$timeZone string The time zone to be normalized
return string, false The PHP time zone identifier, or false if it could not be determined

secondsToHumanTimeDuration() public static method #

public static string secondsToHumanTimeDuration ( \craft\helpers\int $seconds, \craft\helpers\bool $showSeconds true )
$seconds integer The number of seconds
$showSeconds boolean Whether to output seconds or not

secondsToInterval() public static method #

Creates a DateInterval object based on a given number of seconds.

public static DateInterval secondsToInterval ( \craft\helpers\int $seconds )
$seconds integer

timeZoneAbbreviation() public static method #

Returns the timezone abbreviation for a given timezone name.

public static string timeZoneAbbreviation ( \craft\helpers\string $timeZone )
$timeZone string

timeZoneOffset() public static method #

Returns a given timezone’s offset from UTC (e.g. '+10:00' or '-06:00').

public static string timeZoneOffset ( \craft\helpers\string $timeZone )
$timeZone string

toDateTime() public static method #

Converts a value into a DateTime object.

Supports the following formats:

public static DateTime, false toDateTime ( $value, \craft\helpers\bool $assumeSystemTimeZone false, \craft\helpers\bool $setToSystemTimeZone true )
$value mixed The value that should be converted to a DateTime object.
$assumeSystemTimeZone boolean Whether it should be assumed that the value was set in the system time zone if the timezone was not specified. If this is false, UTC will be assumed. (Defaults to false.)
$setToSystemTimeZone boolean Whether to set the resulting DateTime object to the system time zone. (Defaults to true.)
return DateTime, false The DateTime object, or false if $object could not be converted to one

toIso8601() public static method #

Converts a date to an ISO-8601 string.

public static string, false toIso8601 ( $date )
$date mixed The date, in any format that toDateTime() supports.
return string, false The date formatted as an ISO-8601 string, or false if $date was not a valid date

translateDate() public static method #

Translates the words in a formatted date string to the application’s language.

public static string translateDate ( \craft\helpers\string $str, \craft\helpers\string $language null )
$str string The formatted date string
$language string, null The language code (e.g. en-US, en). If this is null, the current application language will be used.
return string The translated date string