DateTimeHelper

Type
Class
Namespace
craft\helpers
Inherits
craft\helpers\DateTimeHelper
Since
3.0

Class DateTimeHelper

View source

Public Methods

MethodDescription
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 or DateTime::ISO8601 (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 time zone string to a PHP time zone 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()

Signature

public static integer currentTimeStamp ( )

currentUTCDateTime()

Signature

public static DateTime currentUTCDateTime ( )

humanDurationFromInterval()

Returns the interval in a human-friendly string.

View source

Arguments

Returns

string

Signature

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

intervalToSeconds()

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

View source

Arguments

Returns

integer

Signature

public static integer intervalToSeconds ( DateInterval $dateInterval )

isInThePast()

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

View source

Arguments

  • $date (mixed) – The timestamp to check

Returns

boolean – True if the specified date was in the past, false otherwise.

Signature

public static boolean isInThePast ( $date )

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).

View source

Arguments

  • $value (mixed) – The timestamp to check

Returns

boolean – Whether the value is an ISO-8601 date string

Signature

public static boolean isIso8601 ( $value )

isThisMonth()

Returns true if given date is in this month

View source

Arguments

  • $date (mixed) – The timestamp to check

Returns

boolean – True if date is in this month, false otherwise.

Signature

public static boolean isThisMonth ( $date )

isThisWeek()

Returns true if given date is in this week

View source

Arguments

  • $date (mixed) – The timestamp to check

Returns

boolean – True if date is in this week, false otherwise.

Signature

public static boolean isThisWeek ( $date )

isThisYear()

Returns true if given date is in this year

View source

Arguments

  • $date (mixed) – The timestamp to check

Returns

boolean – True if date is in this year, false otherwise.

Signature

public static boolean isThisYear ( $date )

isToday()

Returns true if given date is today.

View source

Arguments

  • $date (mixed) – The timestamp to check

Returns

boolean – True if date is today, false otherwise.

Signature

public static boolean isToday ( $date )

isValidIntervalString()

Returns true if interval string is a valid interval.

View source

Arguments

Returns

boolean

Signature

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

isValidTimeStamp()

View source

Arguments

Returns

boolean

Signature

public static boolean isValidTimeStamp ( $timestamp )

isWithinLast()

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

View source

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 – Whether the $dateString was within the specified $timeInterval.

Signature

public static boolean isWithinLast ( $date, $timeInterval )

isYesterday()

Returns true if given date was yesterday

View source

Arguments

  • $date (mixed) – The timestamp to check

Returns

boolean – True if date was yesterday, false otherwise.

Signature

public static boolean isYesterday ( $date )

normalizeTimeZone()

Normalizes a time zone string to a PHP time zone 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 time zone identifier (UTC, GMT, Atlantic/Azores)

View source

Arguments

  • $timeZone (string) – The time zone to be normalized

Returns

string, false – The PHP time zone identifier, or false if it could not be determined

Signature

public static string, false normalizeTimeZone ( \craft\helpers\string $timeZone )

secondsToHumanTimeDuration()

View source

Arguments

  • $seconds (integer) – The number of seconds
  • $showSeconds (boolean) – Whether to output seconds or not

Returns

string

Signature

public static string secondsToHumanTimeDuration ( \craft\helpers\int $seconds, \craft\helpers\bool $showSeconds = true )

secondsToInterval()

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

View source

Arguments

Returns

DateInterval

Signature

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

timeZoneAbbreviation()

Returns the timezone abbreviation for a given timezone name.

View source

Arguments

Returns

string

Signature

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

timeZoneOffset()

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

View source

Arguments

Returns

string

Signature

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

toDateTime()

Converts a value into a DateTime object. Supports the following formats:

  • An array of the date and time in the current locale's short 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

View source

Arguments

  • $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.)

Returns

DateTime, false – The DateTime object, or false if $object could not be converted to one

Signature

public static DateTime, false toDateTime ( $value, \craft\helpers\bool $assumeSystemTimeZone = false, \craft\helpers\bool $setToSystemTimeZone = true )

toIso8601()

Converts a date to an ISO-8601 string.

View source

Arguments

  • $date (mixed) – The date, in any format that toDateTime() supports.

Returns

string, false – The date formatted as an ISO-8601 string, or false if $date was not a valid date

Signature

public static string, false toIso8601 ( $date )

translateDate()

DEPRECATED

Deprecated in 3.0.6. Use \craft\helpers\craft\i18n\Formatter::asDate() instead.

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

View source

Arguments

Returns

string – The translated date string

Signature

public static string translateDate ( \craft\helpers\string $str, \craft\helpers\string $language = null )

Constants

ConstantDescription
SECONDS_DAYNumber of seconds in a day.
SECONDS_HOURNumber of seconds in an hour.
SECONDS_MINUTENumber of seconds in a minute.
SECONDS_MONTHThe number of seconds in a month.
SECONDS_YEARThe number of seconds in a year.