Craft 2 Class Reference

Class DateTimeHelper

Package
craft.app.helpers
Namespace
Craft
Inheritance
class DateTimeHelper
Since
1.0
Source Code
craft/app/helpers/DateTimeHelper.php

Class DateTimeHelper

Public Methods
Method Description Defined By
currentTimeForDb() DateTimeHelper
currentTimeStamp() DateTimeHelper
currentUTCDateTime() DateTimeHelper
formatTimeForDb() DateTimeHelper
fromString() Returns a UNIX timestamp, given either a UNIX timestamp or a valid strtotime() date string. DateTimeHelper
isIso8601() Determines whether the given value is an ISO-8601-formatted date, as defined by either DateTime::ATOM or DateTime::ISO8601 (with or without the colon between the hours and minutes of the timezone offset). DateTimeHelper
isThisMonth() Returns true if given date is in this month DateTimeHelper
isThisWeek() Returns true if given date is in this week DateTimeHelper
isThisYear() Returns true if given date is in this year DateTimeHelper
isToday() Returns true if given date is today. DateTimeHelper
isValidIntervalString() Returns true if interval string is a valid interval. DateTimeHelper
isValidTimeStamp() DateTimeHelper
nice() Returns a nicely formatted date string for given Datetime string. DateTimeHelper
niceShort() Returns a formatted descriptive date string for given datetime string. DateTimeHelper
secondsToHumanTimeDuration() DateTimeHelper
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. DateTimeHelper
timeFormatToSeconds() Takes a PHP time format string and converts it to seconds. {@see http://www.php.net/manual/en/datetime.formats.time.php} DateTimeHelper
toIso8601() Converts a date to an ISO-8601 string. DateTimeHelper
uiTimestamp() Returns a UI-facing timestamp for a given DateTime object. DateTimeHelper
wasInThePast() Returns true if the specified date was in the past, otherwise false. DateTimeHelper
wasWithinLast() Returns true if specified datetime was within the interval specified, else false. DateTimeHelper
wasYesterday() Returns true if given date was yesterday DateTimeHelper

Method Details

currentTimeForDb() public static method #

public static string currentTimeForDb()
Returns string

currentTimeStamp() public static method #

public static int currentTimeStamp()
Returns int

currentUTCDateTime() public static method #

public static DateTime currentUTCDateTime()
Returns DateTime

formatTimeForDb() public static method #

public static DateTime formatTimeForDb($timeStamp $timeStamp = null)
$timeStamp $timeStamp
Returns DateTime

fromString() public static method #

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

public static string fromString(string $dateString, int $userOffset = null)
$dateString string Datetime string
$userOffset int User's offset from GMT (in hours)
Returns string The parsed timestamp.

isIso8601() public static method #

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

public static boolean isIso8601(mixed $value)
$value mixed The value
Returns bool Whether the value is an ISO-8601 date string

isThisMonth() public static method #

Returns true if given date is in this month

public static bool isThisMonth(int $date)
$date int Unix timestamp
Returns bool True if date is in this month, false otherwise.

isThisWeek() public static method #

Returns true if given date is in this week

public static bool isThisWeek(int $date)
$date int Unix timestamp
Returns bool true if date is in this week, false otherwise.

isThisYear() public static method #

Returns true if given date is in this year

public static bool isThisYear(int $date)
$date int Unix timestamp
Returns bool true if date is in this year, false otherwise.

isToday() public static method #

Returns true if given date is today.

public static bool isToday(string $date)
$date string Unix timestamp
Returns bool true if date is today, false otherwise.

isValidIntervalString() public static method #

Returns true if interval string is a valid interval.

public static bool isValidIntervalString($intervalString $intervalString)
$intervalString $intervalString
Returns bool

isValidTimeStamp() public static method #

public static bool isValidTimeStamp($timestamp $timestamp)
$timestamp $timestamp
Returns bool

nice() public static method #

Returns a nicely formatted date string for given Datetime string.

public static string nice(string $dateString = null)
$dateString string The date string.
Returns string Formatted date string

niceShort() public static method #

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.

public static string niceShort(string $dateString = null)
$dateString string Datetime string or Unix timestamp
Returns string Described, relative date string

secondsToHumanTimeDuration() public static method #

public static string secondsToHumanTimeDuration(int $seconds, bool $showSeconds = true)
$seconds int The number of seconds
$showSeconds bool Whether to output seconds or not
Returns string

timeAgoInWords() public static method #

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:

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.

public static string timeAgoInWords($dateTime $dateTime, array $options = array (
))
$dateTime $dateTime
$options array Default format if timestamp is used in $dateString
Returns string The relative time string.

timeFormatToSeconds() public static method #

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

public static string timeFormatToSeconds($timeFormatString $timeFormatString)
$timeFormatString $timeFormatString
Returns string

toIso8601() public static method #

Converts a date to an ISO-8601 string.

public static string|false toIso8601(mixed $date)
$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

uiTimestamp() public static method #

Returns a UI-facing timestamp for a given DateTime object.

public static string uiTimestamp(DateTime $dateTime)
$dateTime DateTime The DateTime object to be formatted.
Returns string The timestamp.

wasInThePast() public static method #

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

public static bool wasInThePast(mixed $date)
$date mixed The datestring (a valid strtotime) or unix timestamp to check.
Returns bool true if the specified date was in the past, false otherwise.

wasWithinLast() public static method #

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

public static bool wasWithinLast(mixed $timeInterval, mixed $dateString, int $userOffset = null)
$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 int User's offset from GMT (in hours)
Returns bool Whether the $dateString was within the specified $timeInterval.

wasYesterday() public static method #

Returns true if given date was yesterday

public static bool wasYesterday(int $date)
$date int Unix timestamp
Returns bool true if date was yesterday, false otherwise.