How to show events for "This Weekend, Next Week"

16

3

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.

Mark Busnelli Jr

Posted 2014-06-13T22:03:15.840

Reputation: 671

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 – 2014-06-13T22:19:07.957

Answers

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

Victor In

Posted 2014-06-13T22:03:15.840

Reputation: 6 831

1Beat me to it :) And I had no idea you could use weekday names in date_modify! Very cool. – Brandon Kelly – 2014-06-13T22:34:14.933

I also just found out after reading the docs, php still surprises from time to time. – Victor In – 2014-06-13T22:35:11.463

5"Next weekend in one year, half an hour ago" — finally, I don't have to calculate this manually all the time! – Matt Stein – 2014-06-13T22:57:13.230

How would you get the Dates in a Range though? You solution above works well for getting events on a certain day but a Weekend should show events from Friday through Sunday right? – Mark Busnelli Jr – 2014-06-14T19:23:54.017

1

Does this help: http://craftcms.stackexchange.com/questions/47/how-can-i-use-operators-within-twig-hashes ?

– Victor In – 2014-06-14T19:30:33.267

1I'll take a look, looks like that post would get me what I'm after :) Thanks for all of the help! – Mark Busnelli Jr – 2014-06-17T23:43:33.793