Skip Navigation

[Résolu] Conditional output on a date + hour

Ce fil est résolu. Voici une description du problème et la solution proposée.

Problem:
The user has created a filter on datetime custom field using TODAY() but it does not work well for posts that have a time

Solution:
The user should use the NOW() function or the SECONDS_FROM_NOW(0) function as he is using datetime field.
TODAY() function works well for date fields.

Relevant Documentation:
https://toolset.com/documentation/user-guides/views/date-filters/

This support ticket is created Il y a 3 années et 6 mois. 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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

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

Supporter timezone: Africa/Casablanca (GMT+00:00)

This topic contains 6 réponses, has 2 voix.

Last updated by Pat Il y a 3 années et 6 mois.

Assisted by: Jamal.

Auteur
Publications
#1797945

Pat

Hello,

I'm trying to place a conditional output based on a date + hour. This is used to display the buy button only after a specific date (which is a custom field 'wpcf-date-prevente').
Here is my condiiton :
[wpv-conditional if="( $(wpcf-date-prevente) lte 'TODAY()' )"]
My custom field is a date + hour field. I found an issue when the date is the current date :
- If the hour is 00 00, then it works and the button is displayed.
- If anything else than 00 00 for the hour, then nothing is displayed.

Could you check and tell me if it is possible to use a date / hour field to manage this condition?

Regards
Pat

#1798719

Jamal
Supporter

Languages: Anglais (English ) Français (Français )

Timezone: Africa/Casablanca (GMT+00:00)

Hello Pat and thank you for contacting the Toolset support.

TODAY() will always return today's date at 00:00:00. Any value of "date-prevente" that has time in today will be greater than TODAY().

I'll suggest that you substruct a day in seconds from the value of "date-prevente" and compare it, the following code should work:

[wpv-conditional if="( ( $(wpcf-date-prevente) - 86400 ) lte 'TODAY()' )"]

Let me know if this is not working.

#1798803

Pat

Hi Jamal,

Thanks for your answer.
The issue with your proposal is that I need to use the date / time that is stored in the custom field. The aim is to allow the buying of a product after a certian date / hour that is defined thanks to this custom field. For info, this date / time is not the same for all products, so I cannot use your proposal that uses the same delta for all products !

Can you explain how th "TODAY" parameter is managed? Is it only a date or could it be a date / time?

Regards
Pat

#1798811

Jamal
Supporter

Languages: Anglais (English ) Français (Français )

Timezone: Africa/Casablanca (GMT+00:00)

TODAY() returns a DateTime, the date of the current day at the time 00:00:00.

Can you elaborate more on the use case? Provide an example so I can understand better?

#1798893

Pat

Hi Jamal,

Let's take this example :
Product A has to be in sales on october, 10th at 10AM
Product B has to be in sales on November, 4th at 4PM

So, I have the custom field "date-of-sales" which is a date field and is part of the products. So, I have set the right values for each product.

Now, I have a product template in which I have placed a conditional output on the buy button thanks to this code : [wpv-conditional if="( $(wpcf-date-of-sales) lte 'TODAY()' )"]
If I place the date with an hour equal to 00 00, this is working. If I try with a specific hour (like 10 00 for example), this is not working ! This sounds as if the fact to use the hour in the custom field is breaking something?

Hope this helps.
Regards
Pat

#1801971

Jamal
Supporter

Languages: Anglais (English ) Français (Français )

Timezone: Africa/Casablanca (GMT+00:00)

Hello Pat,

But, I think, to still need more details about this. What do you mean by it does not work when using another time than 00 00?
Can activate debug on the shortcode and check the debug info:

[wpv-conditional if="( $(wpcf-date-of-sales) lte 'TODAY()' )" debug="true"]

Since, you are using datetime on your custom field, why not using a function that will return the time such as SECONDS_FROM_NOW(int)

[wpv-conditional if="( $(wpcf-date-of-sales) lte 'SECONDS_FROM_NOW(0) ' )"]
#1802027

Pat

Hi Jamal,
Thanks for helping.

The parameter 'SECONDS_FROM_NOW(0) ' is working fine for me. Sorry, I did not see the documentation on this at that page : https://toolset.com/documentation/user-guides/views/date-filters/
I think the NOW() parameter should work fine, anyway, the SECONDS_FROM_NOW(0) fully work for my case.
I also see that the TODAY() parameter is blocked at 00h00m, which explains my issue.

Regards
Pat

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