2

This question relates to my previous question on stackexchange about the difference in days between 2 dates. This ended up being the working solution:

{{ date( inschrijven.datumEvenement|date ).diff( now ).days }}

So the same question applies but this time how can I calculate the number of hours left between ( now ) and a future date {{ inschrijven.datumEvenement }}?

1

You can use the date filter to format the DateInterval to your needs, I guess you are looking for the total hours until the event, so you have to do some math.

Because there's still some strange things going on with Craft DateTime objects, do what I recommended in the question you mentioned, convert it to a string in a common format that includes time and timezone information and then use Twig's date function to convert it to a Twig DateTime object.

To get a "DateTime string" in ISO8601 or UNIX format use the iso8601 or getTimestamp method or the date filter with the corresponding parameter.

{% set dateTimeEvent = date(inschrijven.datumEvenement|date('c')) %}
{% set dateTimeNow = date(now.iso8601) %}

{% set dateInterval = dateTimeNow.diff(dateTimeEvent) %}

{% set days = dateInterval|date('%a') %}
{% set hours = dateInterval|date('%h') %}
{% set totalHours = days * 24 + hours %}

{% set upcoming = dateInterval|date('%R') == '+' ? true %}

{% if upcoming %}
    Event start in {{ totalHours }} hours.
{% endif %}
| improve this answer | |

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.