How to add n-days to twig date format within a for-loop?

半城伤御伤魂 提交于 2021-01-29 09:09:59

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!