In above video explain how to deal with DateTime Object on twig page by using date filter.
In twig we have “date” filter to print the date and format the date time.
Lets learn it from some examples
Print the current date and time with mysql date format on twig page
{{ “now” | date(‘Y-m-d H:i:s’) }}
Print current year on twig page
Print current year on twig page
{{ “now” | date(‘Y’) }}
Print the only date on twig page
{{ “now” | date(‘d-m-Y’) }}
Print the current time on twig page
Print the current time on twig page
{{ “now” | date(‘H:i:s’) }}
Create macro to print the date and time in mysql format, using macro we can create reusable components.
{# file — datetime_test/date_time_macro.html.twig #}
{% macro short_date(object, format) %}
{% if object is defined and object is not null %}
{{ object|date(format) }}
{% endif %}
{% endmacro %}
Import your macro in twig template and use it
{% import ‘datetime_test/date_time_macro.html.twig’ as date_time_util %}
{{ date_time_util.short_date(‘‘now’’, ‘d-m-Y H:i:s’) }}
Comments
Post a Comment