17

Stemming from this original post I was able to show Events using a custom date field and successfully got "Today" and "Tomorrow" events to show up.

How would I go about doing some custom Math to show "This Weekend" or "Next Week" events? I'm sure I have to first write a conditional on determine what the current Day is, but not sure the best way to do this.

  • You might be able to unify these ideas into a beautiful macro or plugin. I only started on the macro quest today though, hence this timid comment instead of an authoritative answer. – Matt Stein Jun 13 '14 at 22:19
22

Twigs date_modify filter accepts every string, which is supported by phps strtotime().

These strings are called Relative Formats.

.

Examples:

In the original post you used:

{% set day = date('m/d/Y') %}
{% set nextDay = day|date_modify('+1 day') %}

but with Relative Formats you can also do something like this:

{% set var = day|date_modify('now') %}
{% set var = day|date_modify('10 September 2000') %}
{% set var = day|date_modify('+1 week') %}
{% set var = day|date_modify('+1 week 2 days 4 hours 2 seconds') %}
{% set var = day|date_modify('next Thursday') %}
{% set var = day|date_modify('last Monday') %}
{% set var = day|date_modify('next month') %}

(These are equivalents of the php-doc examples )

.

So this weekend would be:

{% set var = day|date_modify('next Friday') %}

Or Saturday, personally I start my weekends on Thursday, so it's hard to give a good example ;)

Next weekend:

{% set var = day|date_modify('next Friday +7 days') %}

Next weekend in one year, half an hour ago:

{% set var = day|date_modify('next Friday +1 year -30 minutes') %}
| improve this answer | |
  • 1
    Beat me to it :) And I had no idea you could use weekday names in date_modify! Very cool. – Brandon Kelly Jun 13 '14 at 22:34
  • I also just found out after reading the docs, php still surprises from time to time. – Victor In Jun 13 '14 at 22:35
  • 5
    "Next weekend in one year, half an hour ago" — finally, I don't have to calculate this manually all the time! – Matt Stein Jun 13 '14 at 22:57
  • 1
    Does this help: craftcms.stackexchange.com/questions/47/… ? – Victor In Jun 14 '14 at 19:30
  • 1
    I'll take a look, looks like that post would get me what I'm after :) Thanks for all of the help! – Mark Busnelli Jr Jun 17 '14 at 23:43

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.