Skip Navigation

[Resolved] Conditional if custom-date-field is in month

This support ticket is created 7 years, 4 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
- - 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00
- - - - - - -

Supporter timezone: Asia/Ho_Chi_Minh (GMT+07:00)

This topic contains 2 replies, has 2 voices.

Last updated by dianaM-4 7 years, 4 months ago.

Assisted by: Beda.

Author
Posts
#466062

I am trying to build a shortcode for comparing a custom-date-field to a specific time-period, to check for that in a conditionial-if in one of my views.

I found this thread: https://toolset.com/forums/topic/filter-by-month-and-day/
But I'm not able to modify it, the way I need.

I want to check, if the date is between 1st of May and 31st of October, regardless the year.

Please could you help me with that?
Thanks, Diana

#466179

Custom Date Fields in Types store Timestamps, wich are seconds from the Timestamp Start moment.

So it can not really be said, this is a month and this a day and so on.

It's all calculated in seconds from xy.

But it should be possible to adapt Minesh's Code.

What he does, he creates 3 variables:
//This is the Value saved in the Database (your Date Field)
$val = get_post_meta( $post->ID, 'wpcf-ordination', true );

//This is the month value, got from the above "full date". We get the month with 'n'. It's a number from 1 to 12
$mon = date('n',$val);

//Same here, this is the day, number of the day in the month, without leading 0
$day = date('j',$val);
hidden link

So, what you need is to change the above $mon and $day variables and pass the number you exactly need.
Since you have a max and min value, you just need to compare those with a proper date format in PHP.
Like:
$val = get_post_meta( $post->ID, 'wpcf-ordination', true );
$mon = date('n',$val);//get month

$day = date('j',$val);//get day

$min_day = 1;
$max_day = 31;
$min_month = 6;
$max_monh = 9;

Then you compare if the $mon and day are greater or equal, and smaller or equal to your above set new variables (min and max)

#466284

Thanks, Beda,
that helped a lot to understand the code!
Because I only had to check for the month, I didn't have to compare the days ... Made it easier for me!

Thanks again!
Diana

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