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

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

Started by Harald 6 years ago · 1 replies · 455 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 Anna, 23 hours ago
2 70 2 hours ago
Support · by gtx, 1 week ago
4 233 2 days ago
Support · by Paul Hodges, 2 weeks ago
13 283 5 days ago
Support · by Anna, 3 weeks ago
9 340 1 week ago
Support · by TomW, 2 weeks ago
4 192 2 weeks ago