Skip Navigation

[Resolved] Conditional Statement to Compare individual parts of a Start Date and End Date

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
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10: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: Asia/Kolkata (GMT+05:30)

This topic contains 6 replies, has 2 voices.

Last updated by nedG 3 weeks, 3 days ago.

Assisted by: Minesh.

Author
Posts
#2779141

I have some Events with a Start Date and End Date. When Displaying the Dates, I use several conditional Statements to determine which final display layout of the date that I want.

For Example, if the Start Date and End Date are both the same day (ie... October 15th, 2024) then I only want to display one single date... October 24th, 2024

However if the Start Date and End Date span over 2 different months or 2 different years, I need to display both the Start and End Date separately.

Here is the conditional Statement that I use to compare the Months and years...

In one particular case the start date is May 5, 2022 and the End Date is also May 5, 2022....
[code]

[wpv-conditional if="( '[types field='end-date' style='text' format='F' style='text'][/types]' <> '[types field='start-date' style='text' format='F' style='text'][/types]' ) OR ( '[types field='end-date' style='text' format='Y' style='text'][/types]' <> '[types field='start-date' style='text' format='Y' style='text'][/types]' )" debug="true"]
[types field='start-date' style='text' format='F j'][/types] - [types field='end-date' style='text' format='F j'][/types], [types field='end-date' style='text' format='Y'][/types]
[/wpv-conditional]

[/code]

The debug result is...

####################
wpv-conditional attributes
####################
Array
(
[if] => ( 'May' ne 'May' ) OR ( '2022' ne '2022' )
[debug] => true
)

####################
Debug information
####################
--------------------
Original expression: ( 'May' ne 'May' ) OR ( '2022' ne '2022' )
--------------------
After replacing 1 general variables and comparing strings: ( 'May' ne 'May' ) OR ( '2022' ne '2022' )
Comparing May to May
After replacing 2 general variables and comparing strings: ( 'May' ne 'May' ) OR ( 2022 ne 2022 )
Comparing 2022 to 2022

And on the screen it displays May 5 – 5, 2022

Why is the debug saying this is "true"

#2779149

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

In this case:

In one particular case the start date is May 5, 2022 and the End Date is also May 5, 2022....

What is your expected result. Can you please share admin access details and problem URL.

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.

I have set the next reply to private which means only you and I have access to it.

#2779352

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

You shared:

However if the Start Date and End Date span over 2 different months or 2 different years, I need to display both the Start and End Date separately.

===>
Can you please tell me with what "exhibition" post I should check against which have start and end date configured as different dates in terms of month and year.

When I checked the exhibition posts:
- hidden link

I see all posts are configured with start date and end date with same month and year.

#2779362

The Exhibitions Display page on the front end is here...
hidden link

Look at the Exhibition called Four of a kind

The start date is August 24, 2022 and the end date is August 24, 2022

Therefore, due to the logic in my conditional statements the front end should display it as ONLY August 24, 2022 (which it does correctly).... However it also displays it as August 24 – 24, 2022.. which is incorrect (that conditional statement should be FALSE.. but it ends up as TRUE).

####################
wpv-conditional attributes
####################
Array
(
    [if] => ( 'August' ne 'August' ) OR ( '2022' ne '2022' )
    [debug] => true
)

####################
Debug information
####################
--------------------
Original expression: ( 'August' ne 'August' ) OR ( '2022' ne '2022' )
--------------------
After replacing 1 general variables and comparing strings: ( 'August' ne 'August' ) OR ( '2022' ne '2022' )
	Comparing August to August
After replacing 2 general variables and comparing strings: ( 'August' ne 'August' ) OR ( 2022 ne 2022 )
	Comparing 2022 to 2022
#2779363
conditional.png

Here are the 3 conditional statement that I use to compare dates....

[wpv-conditional if="( '[types field='end-date' style='text' format='F j'][/types]' eq '[types field='start-date' style='text' format='F j'][/types]' )"]
  [types field='start-date' style='text' format='F j'][/types], [types field='end-date' style='text' format='Y'][/types]
  [/wpv-conditional]
  
  [wpv-conditional if="( '[types field='end-date' style='text' format='F'][/types]' eq '[types field='start-date' style='text' format='F'][/types]' )" debug="false"]
  [types field='start-date' style='text' format='F j'][/types] - [types field='end-date' style='text' format='j'][/types], [types field='end-date' style='text' format='Y'][/types]
  [/wpv-conditional]
  
  [wpv-conditional if="( '[types field='end-date' style='text' format='F' style='text'][/types]' ne '[types field='start-date' style='text' format='F' style='text'][/types]' ) OR ( '[types field='end-date' style='text' format='Y' style='text'][/types]' ne '[types field='start-date' style='text' format='Y' style='text'][/types]' )" debug="true"]
  [types field='start-date' style='text' format='F j'][/types] - [types field='end-date' style='text' format='F j'][/types], [types field='end-date' style='text' format='Y'][/types]
  [/wpv-conditional]

For the example above of the "Four of a kind" Exhibition starting and ending on August 24, 2022, only the 1st conditional statement should be TRUE

#2779765

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Can you please check now: hidden link

With your view, I've adjsuted the conditional statement as given under:
- hidden link

[wpv-conditional if="( '[types field='end-date' style='text' format='F j'][/types]' eq '[types field='start-date' style='text' format='F j'][/types]' )"]
  [types field='start-date' style='text' format='F j'][/types], [types field='end-date' style='text' format='Y'][/types]
  [/wpv-conditional]
  
  
  [wpv-conditional if="( '[types field='end-date' style='text' format='F' style='text'][/types]' eq '[types field='start-date' style='text' format='F' style='text'][/types]' ) AND ( '[types field='end-date' style='text' format='Y' style='text'][/types]' eq '[types field='start-date' style='text' format='Y' style='text'][/types]' ) AND ( '[types field='end-date' style='text' format='d' style='text'][/types]' ne '[types field='start-date' style='text' format='d' style='text'][/types]' ) " debug="true"]
  

Can you please confirm it works as expected.

#2779786

Those conditionals don't solve all of the possibilities that I need.

But I was able to see what you did and where my error was. I ended up using...

[wpv-conditional if="( '[types field='end-date' style='text' format='F j'][/types]' eq '[types field='start-date' style='text' format='F j'][/types]' )"]
  [types field='start-date' style='text' format='F j'][/types], [types field='end-date' style='text' format='Y'][/types]
  [/wpv-conditional]
   
  [wpv-conditional if="( '[types field='end-date' style='text' format='F'][/types]' eq '[types field='start-date' style='text' format='F'][/types]' ) AND ( '[types field='end-date' style='text' format='j'][/types]' ne '[types field='start-date' style='text' format='j'][/types]' ) " debug="false"]
  [types field='start-date' style='text' format='F j'][/types] - [types field='end-date' style='text' format='j'][/types], [types field='end-date' style='text' format='Y'][/types]
  [/wpv-conditional]
   
  [wpv-conditional if="( '[types field='end-date' style='text' format='F'][/types]' ne '[types field='start-date' style='text' format='F'][/types]' ) OR ( '[types field='end-date' style='text' format='Y'][/types]' ne '[types field='start-date' style='text' format='Y'][/types]' )" debug="false"]
    [wpv-conditional if="( '[types field='end-date' style='text' format='Y'][/types]' eq '[types field='start-date' style='text' format='Y'][/types]' )" debug="false"]	
  [types field='start-date' style='text' format='F j'][/types] - [types field='end-date' style='text' format='F j'][/types], [types field='end-date' style='text' format='Y'][/types]
  	[/wpv-conditional]
    [wpv-conditional if="( '[types field='end-date' style='text' format='Y'][/types]' ne '[types field='start-date' style='text' format='Y'][/types]' )" debug="false"]	
  [types field='start-date' style='text' format='F j'][/types], [types field='start-date' style='text' format='Y'][/types] - [types field='end-date' style='text' format='F j'][/types], [types field='end-date' style='text' format='Y'][/types]
  	[/wpv-conditional]
  [/wpv-conditional]

This takes care of all of my Start & End Date combinations and how they should be displayed.

Thanks