Skip to content
Grav 2.0 is officially stable. Read the announcement →

Community guidelines

Please keep discussions civil and on-topic. Repeated violations may lead to a temporary ban.

Support

Display text (or activate script) on a certain range of days

Started by Harald 6 years ago · 1 replies · 366 views
6 years ago

My problem is simelar to Display text on a certain day?.
I want to show e.g. a Christmas message like "We wish you all..." from 23rd until 26th Dec, and a similar New Year's message at the end of the year.
How do I do this in a way that it triggers automatically every year?
And I also would like to activate a script (Snowfall) for a period of time in winter automatically triggered every year.
A complication here (and for the New Year's message) is that it starts e.g. on 15th Dec this year and ends on 28th Feb next year.
On the platform I formerly used, I had a PHP-based plugin. Could I use (or find) a similar solution in Grav? So far I have no clue how to set up such a thing. :-(

Harald

6 years ago

You could pretty easily convert the plugin for Grav, but Twig works as well. Just do a conditional on "now"|date(), something like:

TWIG
{% set today = "now"|date('m-d') %}
Today is {{ today }}

{% if today > '11-22' or today <= '03-21' %}
  Winter
{% elseif today > '03-21' and today <= '06-21' %}
  Spring
{% elseif today > '06-21' and today <= '09-22' %}
  Summer
{% elseif today > '09-22' and today <= '11-22' %}
  Fall
{% endif %}

The same condition, adapted for your periods, works for adding the JS-script. If the period is variable between years, you'll need to find the date-expression supported by PHP for this variance. If it's very specific, you might need a helper-library, or to convert the plugin after all.

Suggested topics

Topic Participants Replies Views Activity
Support · by Thomas, 1 week ago
2 60 15 hours ago
Support · by Anna, 3 days ago
2 66 18 hours ago
Support · by Justin Young, 19 hours ago
1 33 19 hours ago
Support · by Duc , 1 week ago
2 69 6 days ago
Support · by Colin Hume, 1 week ago
2 61 6 days ago