Pulling Next Upcoming Date / Time from Statamic Grid Field?

481 Views Asked by At

On a Statamic 1.6.7 based site for a theater I want to use a grid field for performances (they have shows which every Saturday night for several weeks ) and I want to show only the next upcoming show.

EXAMPLE SHOW DATA:

show_performances:  
  -   
    g_show_date: 2014-01-30  
    g_show_time: 07:00 PM  
  -  
    g_show_date: 2014-02-31  
    g_show_time: 07:00 PM  
  -   
    g_show_date: 2014-03-31  
    g_show_time: 07:00 PM  

UPDATE No. 1 (1 February 2014)

And here is my code, where I'm trying David S's suggestion to use {{ g_show_date|in_future }}:

{{ show_performances }}  
    {{ if g_show_date|in_future }}   
    <p>{{ g_show_date }} @ {{ g_show_time }}</p>  
    {{ endif }}  
{{ /show_performances }}  

Which works fine but it shows all future shows (both 2014-02-31 and 2014-03-31). I tried wrapping the output in an {{ if first }} conditional which — as expected — still listed subsequent performances.

Any thoughts on how I can limit output to just the next performance, not any subsequent?

UPDATE No. 2 (1 February 2014)

I also tried Curtis' suggestion:

{{ show_performances limit="1" }} 
   {{if "{ g_show_date format='Ymd' }" >= "{ current_date format='Ymd' }"
        AND "{ g_show_time format='Hi'  }" > "{ current_date format='Hi'}"}}  
        <p>{{ g_show_date }} @ {{ g_show_time }}</p>
   {{ endif }}      
{{ /show_performances }}

but the {{ current_date }} conditional seems to fails: as past performances are returned.

2

There are 2 best solutions below

1
On

I'm not sure this solves your problem, but I think I spotted a typo in your code. In your example it says:

if g_show_date|is_future

Where I got a better result changing it to

if g_show_date|in_future

To have your code show only one next upcoming occurrence of each show I guess you would cycle through your loop with the next show as a condition. (not sure how to accomplish that one right now though)

3
On

You can use {{ current_date }} and the format parameter to test if the time has passed yet.

You might need to play around with the number of curly braces within the if tag; Tags are supposed to use single curly braces within other tags, but I've found it doesn't always work that way. I think someone (Gareth, maybe?) said they noticed it was native Statamic variables use single curlies, while custom variables use double.

{{ entries:listing folder="shows" limit="3" }}
  {{ show_time_grid limit="1" }}

    {{ if { g_show_date format="Ymd" } >= { current_date format="Ymd" } AND
          { g_show_time format="Hi"  } >  { current_date format="Hi"  }
    }}  
      {{ g_show_date }}<br/>
      <h2>{{ title }}</h2>
    {{ endif }}

  {{ /show_time_grid }}
{{ /entries:listing }}

The time format is using leading zeroes for months, days, and hours. Hours are 24-hour time so it doesn't behave unexpectedly with pre/post-noon showtimes. See the PHP manual for more information.