DateTimeHelper

Type
Class
Namespace
Craft
Inherits
Craft\DateTimeHelper
Since
1.0

Class DateTimeHelper

See also http://craftcms.com

View source (opens new window)

# Public Methods

Method Description
currentTimeForDb()
currentTimeStamp()
currentUTCDateTime()
formatTimeForDb()
fromString() Returns a UNIX timestamp, given either a UNIX timestamp or a valid strtotime() date string.
isIso8601() Determines whether the given value is an ISO-8601-formatted date, as defined by either {@link http://php.net/manual/en/class.datetime.php#datetime.constants.atom DateTime::ATOM} or {@link http://php.net/manual/en/class.datetime.php#datetime.constants.iso8601 DateTime::ISO8601} (with or without the colon between the hours and minutes of the timezone offset).
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()
nice() Returns a nicely formatted date string for given Datetime string.
niceShort() Returns a formatted descriptive date string for given datetime string.
secondsToHumanTimeDuration()
timeAgoInWords() Returns either a relative date or a formatted date depending on the difference between the current time and given datetime. $datetime should be in a strtotime-parsable format, like MySQL's datetime datatype.
timeFormatToSeconds() Takes a PHP time format string and converts it to seconds.
toIso8601() Converts a date to an ISO-8601 string.
uiTimestamp() Returns a UI-facing timestamp for a given {@link DateTime} object.
wasInThePast() Returns true if the specified date was in the past, otherwise false.
wasWithinLast() Returns true if specified datetime was within the interval specified, else false.
wasYesterday() Returns true if given date was yesterday

# currentTimeForDb()

Signature

public static string currentTimeForDb ( )

# currentTimeStamp()

Signature

public static integer currentTimeStamp ( )

# currentUTCDateTime()

Signature

public static Craft\DateTime currentUTCDateTime ( )

# formatTimeForDb()

View source (opens new window)

Arguments

  • $timeStamp

Returns

Craft\DateTime

Signature

public static Craft\DateTime formatTimeForDb ( $timeStamp = null )

# fromString()

Returns a UNIX timestamp, given either a UNIX timestamp or a valid strtotime() date string.

View source (opens new window)

Arguments

Returns

string (opens new window) – The parsed timestamp.

Signature

public static string fromString ( $dateString, $userOffset = null )

# isIso8601()

Determines whether the given value is an ISO-8601-formatted date, as defined by either {@link http://php.net/manual/en/class.datetime.php#datetime.constants.atom DateTime::ATOM} or {@link http://php.net/manual/en/class.datetime.php#datetime.constants.iso8601 DateTime::ISO8601} (with or without the colon between the hours and minutes of the timezone offset).

View source (opens new window)

Arguments

  • $value (mixed) – The value

Returns

boolean (opens new window) – 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 (opens new window)

Arguments

Returns

boolean (opens new window) – 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 (opens new window)

Arguments

Returns

boolean (opens new window) – 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 (opens new window)

Arguments

Returns

boolean (opens new window) – 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 (opens new window)

Arguments

Returns

boolean (opens new window) – 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 (opens new window)

Arguments

  • $intervalString

Returns

boolean (opens new window)

Signature

public static boolean isValidIntervalString ( $intervalString )

# isValidTimeStamp()

Signature

public static boolean isValidTimeStamp ( $timestamp )

# nice()

Returns a nicely formatted date string for given Datetime string.

View source (opens new window)

Arguments

Returns

string (opens new window) – Formatted date string

Signature

public static string nice ( $dateString = null )

# niceShort()

Returns a formatted descriptive date string for given datetime string. If the given date is today, the returned string could be "Today, 6:54 pm". If the given date was yesterday, the returned string could be "Yesterday, 6:54 pm". If $dateString's year is the current year, the returned string does not include mention of the year.

View source (opens new window)

Arguments

Returns

string (opens new window) – Described, relative date string

Signature

public static string niceShort ( $dateString = null )

# secondsToHumanTimeDuration()

View source (opens new window)

Arguments

Returns

string (opens new window)

Signature

public static string secondsToHumanTimeDuration ( $seconds, $showSeconds = true )

# timeAgoInWords()

Returns either a relative date or a formatted date depending on the difference between the current time and given datetime. $datetime should be in a strtotime-parsable format, like MySQL's datetime datatype. Options:

  • 'format' => a fall back format if the relative time is longer than the duration specified by end
  • 'end' => The end of relative time telling

Relative dates look something like this: 3 weeks, 4 days ago 15 seconds ago Formatted dates look like this: on 02/18/2004

The returned string includes 'ago' or 'on' and assumes you'll properly add a word like 'Posted ' before the function output.

View source (opens new window)

Arguments

Returns

string (opens new window) – The relative time string.

Signature

public static string timeAgoInWords ( $dateTime, $options = [] )

# timeFormatToSeconds()

Takes a PHP time format string and converts it to seconds. {@see http://www.php.net/manual/en/datetime.formats.time.php}

View source (opens new window)

Arguments

  • $timeFormatString

Returns

string (opens new window)

Signature

public static string timeFormatToSeconds ( $timeFormatString )

# toIso8601()

Converts a date to an ISO-8601 string.

View source (opens new window)

Arguments

  • $date (mixed) – The date, in any format that {@link 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

Signature

public static string, false toIso8601 ( $date )

# uiTimestamp()

Returns a UI-facing timestamp for a given {@link DateTime} object.

  • If the date/time is from today, only the time will be returned in a localized format (e.g. “10:00 AM”).
  • If the date/time is from yesterday, “Yesterday” will be returned.
  • If the date/time is from the last 7 days, the name of the day will be returned (e.g. “Monday”).
  • Otherwise, the date will be returned in a localized format (e.g. “12/2/2014”).

View source (opens new window)

Arguments

Returns

string (opens new window) – The timestamp.

Signature

public static string uiTimestamp ( Craft\DateTime $dateTime )

# wasInThePast()

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

View source (opens new window)

Arguments

  • $date (mixed) – The datestring (a valid strtotime) or unix timestamp to check.

Returns

boolean (opens new window) – True if the specified date was in the past, false otherwise.

Signature

public static boolean wasInThePast ( $date )

# wasWithinLast()

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

View source (opens new window)

Arguments

  • $timeInterval (mixed) – The numeric value with space then time type. Example of valid types: 6 hours, 2 days, 1 minute.
  • $dateString (mixed) – The datestring or unix timestamp to compare
  • $userOffset (integer (opens new window)) – User's offset from GMT (in hours)

Returns

boolean (opens new window) – Whether the $dateString was within the specified $timeInterval.

Signature

public static boolean wasWithinLast ( $timeInterval, $dateString, $userOffset = null )

# wasYesterday()

Returns true if given date was yesterday

View source (opens new window)

Arguments

Returns

boolean (opens new window) – True if date was yesterday, false otherwise.

Signature

public static boolean wasYesterday ( $date )