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 Answer 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
    Commented Oct 13, 2017 at 19:39
  • Something isn’t right, you should not need to factor in any timezone differences.
    – carlcs
    Commented Oct 14, 2017 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
    Commented Oct 21, 2017 at 12:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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