简体   繁体   中英

How to add days to a variable date in twig

A few days ago the need to add variables in twig, to be precise having a date for example: 2016-01-04 I need to add 540 days for this I use the date_modify plugin present in twig.

To perform this operation you must perform the following:

 {% In September modify = '540'%}
 {% In September date = "now" | date ( "m / d / Y")%}

 {{Date | date_modify ( "+" ~ ~ modify "day") | date ( "m / d / Y")}}

With this the result would be: 08/31/2017

Do you know another way?

{{ yourDate|date_modify("+1 day")|date("m/d/Y") }}

this code add 1 day to your date.

see http://twig.sensiolabs.org/doc/filters/date_modify.html

This worked for me:

{% set tomorrow = 'now'|date_modify("+1 day") %}
<input id="event_date" name="event_date" class="datepicker" type="date" value="{{ tomorrow|date('Y-m-d') }}" />

Just in case someone needs to add more than +1 day in a twig template

{% set sevenDays = date(yourDate)|date_modify("+7 days")|date("d/m/Y")%}

Once the variable set can be used anywhere in the template

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM