<p>Yes, you can do that in Twig. Twig compiles down to PHP, which gets executed for every request. Craft doesn't do any inherent caching, you have to explicitly ask it to cache things via the <code>{% cache %}</code> tag, so the example you posted would work fine.</p>

<p>If you want to learn more about the <code>{% cache %}</code> tag, check out <a href="https://nystudio107.com/blog/the-craft-cache-tag-in-depth" rel="nofollow noreferrer">The Craft {% cache %} Tag In-Depth</a></p>

CC BY-SA 4.0
2018-09-05T07:06:07.467Z
2018-09-05T07:06:07.467Z
andrew.welch
<p>Using the <a href="https://docs.craftcms.com/v2/templating/cache.html" rel="nofollow noreferrer">cache tag,</a> you can control when the cache will expire. </p>

<p>If I understand correctly, the check you are doing will have a different result on a daily basis. You can ask a piece of content to be cached until the end of the day. The first request of the next day will automatically clear the cache and the content will be generated again.</p>

<p>You can define the cache expiration time like this:</p>

<pre><code>{# The cache should expire at the beginning of each day #}
{% set cacheExpirationTime = now|date_modify('+1 day')|date('Y-m-d 00:00')|date('c') %}
</code></pre>

<p>You can then use this variable in the cache tag:</p>

<pre><code>{% cache until cacheExpirationTime %}
    {# Heavy calculations here #}
{% endcache %}
</code></pre>

<p>Please note that if you fetch some content from Craft within the cache tag and this content gets modified, the cache will automatically be cleared before the expiration time.</p>

CC BY-SA 4.0
2018-09-04T12:13:25.637Z
2018-09-04T12:13:25.637Z
kant312
<p>How does the Craft CMS cache work? I want to show a certain message if the server date is between x and y. Can I do this by using an Twig if/else statement or is this cached?</p>

<p>My code is like this: </p>

<pre><code>{% set now = "now"|date("U") %}

{% set tournamentDates = [
    1536323400,
    1536409800,
    1536971400,
    1537650000,
    1537736400
]
%}

{% for tournamentTime in tournamentDates %}

  &lt;!-- {{ tournamentTime|date("d-m-y H:i:s")}} --&gt;

  {% if ( now &gt; tournamentTime ) and ( now &lt; tournamentTime + 43200 ) %}

    TOURNAMENT IS LIVE

  {% endif %}

{% endfor %}
</code></pre>

<templating><cache><datetime>
Caching question
# Caching question

(Asked by: username_0 on 2018-09-04)

How does the Craft CMS cache work? I want to show a certain message if the server date is between x and y. Can I do this by using an Twig if/else statement or is this cached?
My code is like this: 
{% set now = "now"|date("U") %}

{% set tournamentDates = [
    1536323400,
    1536409800,
    1536971400,
    1537650000,
    1537736400
]
%}

{% for tournamentTime in tournamentDates %}

  

  {% if ( now > tournamentTime ) and ( now < tournamentTime + 43200 ) %}

    TOURNAMENT IS LIVE

  {% endif %}

{% endfor %}

**Tags:** templating, cache, datetime


---

## 2 answers

### Answer by username_1 on 2018-09-05. Score: 1 | Accepted answer

Yes, you can do that in Twig. Twig compiles down to PHP, which gets executed for every request. Craft doesn't do any inherent caching, you have to explicitly ask it to cache things via the `{% cache %}` tag, so the example you posted would work fine.

If you want to learn more about the `{% cache %}` tag, check out [The Craft {% cache %} Tag In-Depth](https://nystudio107.com/blog/the-craft-cache-tag-in-depth)


---

### Answer by username_2 on 2018-09-04. Score: 1

Using the cache tag, you can control when the cache will expire. 
If I understand correctly, the check you are doing will have a different result on a daily basis. You can ask a piece of content to be cached until the end of the day. The first request of the next day will automatically clear the cache and the content will be generated again.
You can define the cache expiration time like this:
{# The cache should expire at the beginning of each day #}
{% set cacheExpirationTime = now|date_modify('+1 day')|date('Y-m-d 00:00')|date('c') %}

You can then use this variable in the cache tag:
{% cache until cacheExpirationTime %}
    {# Heavy calculations here #}
{% endcache %}

Please note that if you fetch some content from Craft within the cache tag and this content gets modified, the cache will automatically be cleared before the expiration time.


---

