Question about If / Else and date comparisons

Hi there,

I previously used Custom Content Shortcode to display different Beaver Builder saved layouts depending on the time using the following:-

[if field=wpcf-online-event-date after=today in=timestamp]
    [fl_builder_insert_layout id="2216"]
[else]
    [fl_builder_insert_layout id="2217"]
[/if]

How would I achieve the same in Loops and Logic?

Hi Craig,

A bit randomly, can you try this?

<If field=wpcf-online-event-date more_than today>

[Update]

I have tested with an ACF Date Picker for the event date, it doesnā€™t work: either the syntax above is wrong or dates canā€™t be compared (different formats) or a feature is not implemented orā€¦

Hi @craig,

At the moment, the If tag doesnā€™t have date comparisons yet - itā€™s been requested before, so we do plan to implement it.

Until then, I think itā€™s still possible with the Date tag.

<If field=wpcf-online-event-date more_than value="{Date format=timestamp}">

Ah, but that checks if event is after now, not after today. So we need the timestamp for the end of today.

How aboutā€¦

<Set end_of_today>
  <Date format=timestamp><Date format=Y-m-d /> 23:59:59</Date>
</Set>

<If field=wpcf-online-event-date more_than value="{Get end_of_today}">
  ...
</If>

This creates a timestamp for the end of today, and compares if the field value is more than that. Could you see if that works?


@avanti - I like how your suggested code looks. It should ideally work as simple as that, except maybe using an attribute after, to differentiate it from comparing the literal text, like value=today.


OK, Iā€™ve made a note to improve date comparisons, and will return to this discussion topic to write an update.

Hi @eliot,

Thanks for the detailed suggestions.
The many examples in CCS docs are key helpers for non developper, they were precious starting points to me, iā€™m glad that L&L follows the same way.

I also discover these features in your post, :

{Date format=timestamp} with the curly brackets, to fill a value

ā€¦and the implementation of Set/Get, with {Getā€¦} to fill a value too

Thatā€™s great.

Unfortunately i wasnā€™t able to make these work in a loop.
Hereā€™s my code, with an ACF Date Picker on Posts to set the Event date:

<Loop type=post>
  
  <h4><Field title /></h4>
  
  <If field="posts_event_date">
    Event date: <Field acf_date=posts_event_date format="Y-m-d" /><br>
  </If>
    
  <If field="posts_event_date" more_than value="{Date format=timestamp}">
    <span style="color: red;">Upcoming Event</span>
  </If>
  
</Loop>

I checked twice, despite one of the post has a future date set in the ACF, the second condition in the loop is not true and doesnā€™t output ā€œUpcoming Eventā€ for this post as it should.

Maybe dates formats are not similar and comparable, between ACF and {Date format=timestamp}, iā€™m not sure.

Iā€™ve set the ACF Date to output Y-m-d

Capture dā€™eĢcran 2021-03-06 aĢ€ 18.22.16

Hi @avanti,

Right, itā€™s about different dates formats. Currently, the more_than operator only knows how to compare numbers - which means given values need to be formatted as timestamps.

Another thing which is different from CCS is the use of attribute acf_date instead just field. That lets the plugin know to use ACF to get the value, and process it as a date; itā€™s more reliable than using the raw field value.

The following will probably work as expected:

<If check="{Field acf_date=posts_event_date format=timestamp}" more_than value="{Date format=timestamp}">

Iā€™m still trying to decide a new syntax, how to express a condition like this in a shorter way.


Glad youā€™re enjoying the newly improved details of Loops and Logic.

Good point about the usefulness of examples - I imagine many will accumulate here in the forums, but it would be nice to have a community library of code snippets. Iā€™ve made a note to think about how to build it.

Hi @eliot,

The code you propose works alright, thanks a lot for taking the time to test and share.
In this example i discover the usage of check, looks like a flexible function.

@craig Hereā€™s my code that works finally:

<Loop type=post>
  
  <h4><Field title /></h4>
  
  <If field="posts_event_date">
    Event date: <Field acf_date=posts_event_date format="Y-m-d" /><br>
  </If>
    
  <Set end_of_today>
    <Date format=timestamp><Date format="Y-m-d" /> 23:59:59</Date>
  </Set>

  <If check="{Field acf_date=posts_event_date format=timestamp}" more_than value="{Get end_of_today}">
  
</Loop>

Wow! You gents have been busy over the weekend!

Thank you both very much for taking the time to explore and explain these functions. Thereā€™s quite a lot to take in, but I will take a look at testing these myself later on, as I have some other pressing work to be getting on with right now.

I will also report back and let you know how I got on.

1 Like

Hi Craig,

Itā€™s been a long time, but Iā€™ve finally added date conditionals - documented here: Date conditionals | Tangible Loops & Logic

The following CCS shortcode:

[if field=wpcf-online-event-date after=today in=timestamp]
    [fl_builder_insert_layout id="2216"]
[else]
    [fl_builder_insert_layout id="2217"]
[/if]

Can be expressed in L&L as:

<If field=wpcf-online-event-date after=today>
    <Shortcode fl_builder_insert_layout id=2216 />
<Else />
    <Shortcode fl_builder_insert_layout id=2217 />
</If>

Depending on what date format the field value is in, it might be necessary to convert it to timestamp. Please see the documentation page for details.

4 Likes

Useful enhancement, thank you.

1 Like

Hey @eliot, that is fantastic, thank you so much for adding these conditionals. Really, really helpful.

1 Like