2

The goal is to show a word when date field qualifies for tomorrow or today instead of numeric date.

Setting and calling variables:

{% set deliveryDate = order.deliveryDate %}
{% set tomorrow = now|date_modify("+ 1 day") %}

{% if deliveryDate == tomorrow %}
  {% set deliveryDate = "tomorrow" %}
{% elseif deliveryDate == now %}
  {% set deliveryDate = "today" %}
{% else %}
  {% set deliveryDate = deliveryDate|date("d.m.Y") %}
{% endif %}

My date field has selected value of tomorrow whereas the first statement should be truthy.

So I think I'm comparing two objects with the same values but different classes? I had figured it out by formatting the date objects to strings using day() filter.

But why is THAT happening on the first place?

2

now represents the current DateTime so unless you specify exactly the correct time in your field the condition deliveryDate == now will never become true. If the user visits the page one second earlier or one second later the condition will fail.

For example: if your date is 2018-06-13 12:00:00 and now is 2018-06-13 12:00:01 both of your conditions will always fail. now|date_modify("+ 1 day") will result in 2018-06-14 12:00:01 and 12:00:00 is not 12:00:01

you'll need to equalize the time or format it differently

{% if deliveryDate.modify('midnight') == tomorrow.modify('midnight') %}

or

{% if deliveryDate.format('Y.m.d') == tomorrow.format('Y.m.d') %}

You can read more about it in the docs

| improve this answer | |
  • Understand @robin I just had no idea that a time parameter is included within now variable. – Dominik Krulak Jun 13 '18 at 14:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.