2

How can I query all completed orders within the last hour? It seems craft is only comparing a date by YYYY-MM-DD.

Following query collects all orders from the same day, as {{dateFrom}} would output 2017-10-13 and not eg. 2017-10-13 11:22:08

{% set dateFrom = now|date_modify('-3600 seconds') %}
{% for order in craft.commerce.orders.dateOrdered( '>= ' ~ dateFrom) %}
  ...
{% endfor %}

adding something like craft.commerce.orders.dateOrdered( '>= ' ~ dateFrom|date('c')) would find any orders at all.

1
'>= ' ~ dateFrom|date('c')

is correct to output the DateTime object as a string with the time (and timezone) included. If you don’t use the filter it’s output in the Y-m-d format by default.

So my recommendation would be to double check what dateFrom|date('c') actually outputs and to compare it with the dateOrdered values in the CP or database.

  • Thanks a lot, of course '>= ' ~ dateFrom|date('c') works perfectly fine.. I didn't get any output before, because (i think) the database time is UTC and crafts dates are UTC+2. So I ended up taking the offset into account by doing this: {% set dateFrom = now|date_modify( ((now|date("Z") + 3600) * (-1)) ~' seconds') %} and .dateOrdered( '>= ' ~ dateFrom|date('c')) – alex_craze Oct 13 '17 at 19:39
  • Something isn’t right, you should not need to factor in any timezone differences. – carlcs Oct 14 '17 at 6:16
  • sry, for my late reply. there is indeed something odd going on between our DB and craft - but as this behavior only occurs on the dev-environment I am just going to ignore this for now. Anyways, the original question was answered perfectly! thx alot. – alex_craze Oct 21 '17 at 12:53

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.