Skip Navigation

[Resolved] I need to create a conditional logic using the today date and the date field

This support ticket is created 4 years, 8 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 14:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Jamaica (GMT-05:00)

Tagged: 

This topic contains 29 replies, has 2 voices.

Last updated by Shane 4 years, 7 months ago.

Assisted by: Shane.

Author
Posts
#1321277

Tell us what you are trying to do? I'm trying to build an Event site.
In the post I have created two date fields, one for the start and one for the end of the event.
So what I need to do is: If the today date is < of the start date the system will show that "the Event will be" message. If today date is >= of the start date and is < of the end date the system will show that "the Event now is coming" message. If today date is >= of the end date the system will show that "the Event is ended" message.
Now, what should I do to have the today date?
And is it correct to use this logic system or there are other methods to do that?

Is there any documentation that you are following? No

Is there a similar example that we can see? I don't know

What is the link to your site? hidden link

#1321343

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Converted

#1321349

Thank you.
So, now I'm using the code you have written before, but it doesn't work fine... The conditional logic show always the message that the event is ended...

#1321417

this is the code that I have used

[wpv-conditional if="( '[data-oggi]' lt '[data-oggi-totimestamp date='[types field='wpcf-data-inizio' index='0'][/types]']' )"]in programma[/wpv-conditional]
[wpv-conditional if="( '[data-oggi]' gte '[data-oggi-totimestamp date='[types field='wpcf-data-inizio' index='0'][/types]']' ) AND ( '[data-oggi]' lt '[data-oggi-totimestamp date='[types field='wpcf-data-fine'][/types]']' )"]in corso[/wpv-conditional]
[wpv-conditional if="( '[data-oggi]' gte '[data-oggi-totimestamp date='[types field='wpcf-data-fine'][/types]']' )"]passato[/wpv-conditional]
#1321545

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Francesco,

I've worked a bit more on this for you to try getting it resolved. Some changes were made to the shortcode to match what you want exactly.

Firstly i've modified the shortcode for today to get the current date as well as time and to convert this to a timestamp.


// Add Shortcode
function wp_get_today() {
	$date = date("h:i:s A");
	return strtotime($date);

}
add_shortcode( 'wp_get_today', 'wp_get_today' );

The example usage will be [wp_get_today].

Secondly i've updated the shortcode to convert your datefield to a timestamp. This update means that you won't need to pass the entire date field shortcode as a parameter but only the actual slug of the field without the wpcf- prefix.


// Add Shortcode
function wp_date_totimestamp( $atts ) {
 
    // Attributes
    $atts = shortcode_atts(
        array(
            'date_field' => '',
        ),
        $atts
    );
 
     $date = do_shortcode("[types field='".$atts['date_field']."' style='text' format='F j, Y g:i a' index='0'][/types]");
    return strtotime($date);
 
}
add_shortcode( 'wp_date_totimestamp', 'wp_date_totimestamp' );

Now how we use this is


[wp_date_totimestamp date_field="datetime"]

In my case my field slug is "datetime".

Finally one example usage in the conditional is


[wpv-conditional if="( '[wp_get_today]' lte '[wp_date_totimestamp date_field="datetime"]' ) "] Test  [/wpv-conditional]

So this checks to see if Todays date is less than or equal to the date time in my field and Displays TEST as long as the condition remains true.

Finally in order to get these shortcodes to work in our conditionals they must be added to the Toolset 3rd party shortcode arguments in Toolset -> Settings -> Frontend .

Please let me know if this helps.

Thanks,
Shane

#1321937

Hi, thank you very much.
So, I have used your code changing "wp_date" with "data-oggi", I have deleted the wpcf- prefix in the datetime field in the shortcode, but all condition I use return me the message that the event is gone...

#1322175

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Francesco,

Could you provide me with admin access to the site as well as a link to the page where you are testing this out ?

Thanks,
Shane

#1322439

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Francesco,

I see the problem.

The view below is listing out from the Comuni CPT, so the field in the view will naturally want to get the date field for that CPT. However there is no date field there. The datefield is actually on the default wordpress Post type.

I checked to see how they are related but there is no post relationship between the two.

Could you clarify here for me?

Thanks,
Shane

#1322455
Giurdignano  8 e 9 agosto la  Sagra du Contadinu    Eventi v2 InOnda.png

Ok, I can do change to the View, but, if you see the attached image, I have used the conditional logic with the datetime shortcode in a module outside the View.
You can test on it

#1322475

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Francesco,

I believe there is something on your site that is causing the shortcodes to loop multiple times or multiple calls are being made.

If you take a look at the debug data below from my shortcode.

string(23) "Agosto 22, 2019 7:30 pm" string(23) "Agosto 22, 2019 7:30 pm" string(23) "Agosto 22, 2019 7:30 pm" string(23) "Agosto 22, 2019 7:30 pm" string(23) "Agosto 22, 2019 7:30 pm" string(0) "" string(0) "" string(0) "" string(0) ""

Its returning the strings multiple times when it should just be doing it once like below.

string(23) "Agosto 22, 2019 7:30 pm"

This is perhaps what is causing the conditionals to not run correctly. On my test site i've tested both shortcodes and they work fine for me.

Thanks,
Shane

#1322489
Giurdignano  8 e 9 agosto la  Sagra du Contadinu    Eventi v2 InOnda 2.png

Thanks...
And so, there are no solutions?

See the attached image, is a screenshot of the top of the site, and there are some display issue

#1322533

I have deactivated all plugins except the Toolset plugins, but the multiple times looping remain...
I don't know what could be...

#1322559

So, I have replaced the code into the custom code section and I think that the multiple time looping is solved.
Can you test it?
But the conditional logic doesn't work fine

#1322959

So, if I write the shortcode

[data-oggi]

in the post, to do a test, I can see the date numbers
but, if I write this shortcode

[data-oggi-totimestamp date='[types field='data-inizio'][/types]']

in the post, to do a test, I have no results...

#1323077

I think that I have found the issue and I have change this custom code

// Add Shortcode
function wp_date_totimestamp( $atts ) {
  
    // Attributes
    $atts = shortcode_atts(
        array(
            'date_field' => '',
        ),
        $atts
    );
  
     $date = do_shortcode("[types field='".$atts['date_field']."' style='text' format='F j, Y g:i a' index='0'][/types]");
    return strtotime($date);
  
}
add_shortcode( 'wp_date_totimestamp', 'wp_date_totimestamp' );

to

// Add Shortcode
function wp_date_totimestamp( $atts ) {
  
    // Attributes
    $atts = shortcode_atts(
        array(
            'date_field' => '',
        ),
        $atts
    );
  
     $date = do_shortcode("[types field='".$atts['date_field']."' style='text' format='Y/m/d H:i' index='0'][/types]");
    return strtotime($date);
  
}
add_shortcode( 'wp_date_totimestamp', 'wp_date_totimestamp' );

And now with the shortcode

[data-oggi-totimestamp date_field='data-inizio']

a result is displayed, but it not calculate the time of the datetime custom field...

The 2019/08/24 19:30 date results should be 1566667800 instead we have 1566675000

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.