<p>You can use the <a href="http://twig.sensiolabs.org/doc/filters/date.html" rel="nofollow"><code>date</code></a> 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.</p>

<p>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 <em>that includes time and timezone information</em>
and then use Twig's <a href="http://twig.sensiolabs.org/doc/functions/date.html" rel="nofollow"><code>date</code></a> function to convert it to a Twig DateTime object.</p>

<p>To get a "DateTime string" in ISO8601 or UNIX format use the <a href="http://buildwithcraft.com/docs/templating/datetime#iso8601" rel="nofollow"><code>iso8601</code></a> or <a href="http://buildwithcraft.com/docs/templating/datetime#getTimestamp" rel="nofollow"><code>getTimestamp</code></a> method or the <a href="http://twig.sensiolabs.org/doc/filters/date.html" rel="nofollow"><code>date</code></a> filter with the <a href="http://php.net/manual/en/function.date.php" rel="nofollow">corresponding parameter</a>.</p>

<pre><code>{% 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 %}
</code></pre>

CC BY-SA 3.0
2015-07-31T12:57:15.570Z
2015-07-31T12:57:15.570Z
carlcs
<p>This question relates to my <a href="https://craftcms.stackexchange.com/questions/9756/how-to-calculate-the-number-of-days-left">previous</a> question on stackexchange about the difference in days between 2 dates. This ended up being the working solution:</p>

<pre><code>{{ date( inschrijven.datumEvenement|date ).diff( now ).days }}
</code></pre>

<p>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 }}?</p>

<templating><datetime>
How to calculate the number of hours left?
# How to calculate the number of hours left?

(Asked by: username_0 on 2015-07-31)

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 }}?

**Tags:** templating, datetime

---

## 1 answers

### Answer by username_1 on 2015-07-31. Score: 1 | Accepted answer

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 %}


---

