Skip Navigation

[Resolved] Help with conditional targeting only part of a date field.

This thread is resolved. Here is a description of the problem and solution.

Problem: I have two custom date fields, one for the start date and one for the end date. If both dates are populated, and they both exist in the same months, then I want to display the date range like 1 - 4 June 2018. If the start and end dates fall in different months, then I want to show them like 28 June - 7 July 2018. So I need to be able to target the month name in a conditional.

Solution:
You can use the Types field shortcode to format the output of the date custom field so that it only includes the month name:

[types field='event-starts-date' format='F'][/types]

You can test those formatted custom field values in a conditional:

[wpv-conditional if="( '[types field='event-starts-date' format='F'][/types]' eq '[types field='event-end-date' format='F'][/types]' )" evaluate="false"]
Start and end date months do not match, so output the first month name here.
[/wpv-conditional]

Relevant Documentation:
https://toolset.com/documentation/customizing-sites-using-php/functions/#date
https://toolset.com/documentation/user-guides/conditional-html-output-in-views/

This support ticket is created 6 years, 6 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
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 2 replies, has 2 voices.

Last updated by diyanK 6 years, 6 months ago.

Assisted by: Christian Cox.

Author
Posts
#914210

Hi,

Here is my situation:

I have Events.
Some events are single day events, others span across more than one day.

I use two date fields, second one is conditionally triggered by a checkbox which asks me if the event is longer than a day.

If it is a single day event front-end only shows the first field (which is called "event-starts-date").
When I define the date of the event I use the checkbox(called "duration") which conditionally triggers the addition of the second date field(called "event-end-date") that will mark the event's end date (if the event is longer than 1 day).

Here is how my code looks:

[wpv-conditional if="( $(wpcf-duration) eq '1' )"]
     [types field='event-starts-date' style='text' format='j F Y'][/types]
[/wpv-conditional]
[wpv-conditional if="( $(wpcf-duration) eq '2' )"]
     [types field='event-starts-date' style='text' format=' j F'][/types] - [types field='event-end-date' style='text' format='j F Y'][/types]
[/wpv-conditional]

All works good and as expected.

In cases when the event spans across multiple days in a single month I get this result:
15 June - 16 June 2018
In cases when the event spans across two months I get this result:
28 June - 3 July 2018

Here is my problem:
I would like to keep the month titles when it spans across multiple months, but would like to remove only the first month title when the event is contained in a single month.
So it should work like this:
15 - 16 June 2018
28 June - 3 July 2018

Is there a way to use a conditional that will target only part of the date custom field, so that I can remove the first month title if it is equal to the month title within the second date custom field?

Thanks for looking into this!

#914457

You can use the Types field shortcode to format the output of the date custom field so that it only includes the month name:

[types field='event-starts-date' format='F'][/types]

You can test those formatted custom field values in a conditional:

[wpv-conditional if="( '[types field='event-starts-date' format='F'][/types]' eq '[types field='event-end-date' format='F'][/types]' )" evaluate="false"]
Start and end date months do not match, so output the first month name here.
[/wpv-conditional]
#914604
Screen Shot 2018-06-18 at 11.32.28.png

Hi Christian,

This is good!

I actually used the evaluate "true" to get it going.

Then I put this new code inside my other conditional that evaluates against being a multiple day event and it worked beautifully!

Here is my result: