问题
I'm working with twig and got the date and format working. I have a start date (let's say todays day) and I'd like to print every day into a table cell.
I have my date field in the var datum and I'm able to add 1 day with this. it's working.
{% set datum = date(current_user.cwmon)|date_modify("+1 day")|date('D d.m.y') %}
when I put this into a for loop, I get not the answer I'd like to.
the code itself:
{% for j in 0..6 %}
{% set datum = date(current_user.cwmon)|date_modify("+1 day")|date('D d.m.y') %}
// other code
{{ j }}: {{ datum }}
// other code
{% endfor %}
is there a way to use my var j instead of +1 day? Whatever I try I get an error.
my desired result:
0: Mon 15.01.19
1: Tue 16.01.19
...
6: Sun 20.01.19
Thank you very much in advance.
回答1:
apparently the answer is quite simple.
{% for j in 0..6 %}
{% set datum = YOUR_DATE|date_modify("+" ~ j ~ " day")|date('D d.m.y') %}
{% endfor %}
with this, datum has the correct value and adds j to itself.
回答2:
Another solution is overwriting the datum variable
{% set datum = current_user.cwmon %}
{% for j in 0..6 %}
{% set datum = date(datum)|date_modify("+1 day")|date('D d.m.y') %}
// other code
{{ j }}: {{ datum }}
// other code
{% endfor %}
demo
来源:https://stackoverflow.com/questions/54216202/how-to-add-n-days-to-twig-date-format-within-a-for-loop