I would like to display a formatted date in twig by applying a filter to a Unix timestamp. Is such a feature available in twig?
You can simply use the fromtimestamp function from the DateTime module to get a date from a UNIX timestamp. This function takes the timestamp as input and returns the corresponding DateTime object to timestamp.
The first thing we need to do is get today. We do that using the now string in Twig. We can pass this string through the date filter and get a string of today's date.
To find the unix current timestamp use the %s option in the date command. The %s option calculates unix timestamp by finding the number of seconds between the current date and unix epoch.
TIMESTAMP has a range of '1970-01-01 00:00:01' UTC to '2038-01-19 03:14:07' UTC. A DATETIME or TIMESTAMP value can include a trailing fractional seconds part in up to microseconds (6 digits) precision. With the fractional part included, the format for these values is ' YYYY-MM-DD hh:mm:ss [.
There is a filter called date
.
In the following example mydate
equals 1286199900
:
{{ mydate|date }} <!-- October 4, 2010 13:45 -->
{{ mydate|date('d/m/Y') }} <!-- 04/10/2010 -->
If you just divide it by 1000 there might be an error like
Failed to parse time string (1384361503.5) at position 7
I would add round function to it
{% set timestamp = (rating.ratingDate / 1000)|round(0, 'floor') %}
{{timestamp|date('d. M Y')}}
I know I am playing an archaeologist but it might be helpful for someone.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With