Timber Logo

You are reading the documentation for Timber v2.x. Switch to the documentation for Timber v1.x.

Timber\​DateTimeHelper

Helper class to work with dates and times.

Overview #

Methods #

NameReturn TypeSummary/Returns
time_ago()stringReturns the difference between two times in a human readable format.
wp_date()false or stringWrapper for wp_date().

Class Methods #

wp_date() #

Wrapper for wp_date().

since 2.0.0

wp_date( null|string|false $format = null, string|int|\DateTimeInterface $date = null, null|\DateTimeZone $timezone = null )

Returns: false|string

NameTypeDescription
$formatnull or string or falseOptional. PHP date format. Will use the date_format option as a default.
$datestring or int or \DateTimeInterfaceA date.
$timezonenull or \DateTimeZoneOptional. Timezone to output result in. Defaults to timezone from site settings.

time_ago() #

Returns the difference between two times in a human readable format.

Differentiates between past and future dates.

see human_time_diff()

time_ago( int|string $from, int|string $to = null, string $format_past = null, string $format_future = null )

Returns: string

NameTypeDescription
$fromint or stringBase date as a timestamp or a date string.
$toint or stringOptional. Date to calculate difference to as a timestamp or a date string. Default current time.
$format_paststringOptional. String to use for past dates. To be used with sprintf(). Default %s ago.
$format_futurestringOptional. String to use for future dates. To be used with sprintf(). Default %s from now.

Twig

{{ post.date('U')|time_ago }}
{{ post.date('Y-m-d H:i:s')|time_ago }}
{{ post.date(constant('DATE_ATOM'))|time_ago }}