0

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.

lechnerio
  • 884
  • 1
  • 16
  • 44

2 Answers2

0

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.

lechnerio
  • 884
  • 1
  • 16
  • 44
0

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

DarkBee
  • 16,592
  • 6
  • 46
  • 58
  • hi. thank you for your answer. in the meantime I found a solution, which works very well for me, I found a way to replace "+1 day" with "+" ~ j ~ " day" to actually add the numbers of loop throughs – lechnerio Jan 16 '19 at 12:03
  • That is why I stated "another solution" - imho using `date_modify("+"~j~" day")` is less readable though – DarkBee Jan 16 '19 at 12:05
  • but it does the job, yours on the other hand is not working. because that caused the same issue as my problem. – lechnerio Jan 17 '19 at 07:41
  • How is mine not working? Watch closely what I did different and please do click the demo before saying mine is not working – DarkBee Jan 17 '19 at 07:46