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’écran 2021-03-06 à 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