Compare variables and set the first that has value

I have an Event lister loop, where I made variables from the Event’s dates and today date. There is a Required $maindate, but there are also other dates of the same event $otherdates, which are later than the main.
I want to set-up the latest date that is set in a separate variable, and compare it to the today’s date to have the if ( $comparedate >= $today ) controlling the listing only the future events.

Here are my variables:

Read More
    $maindate = DateTime::createFromFormat('Ymd', get_field('date'));
    $otherdates1 = DateTime::createFromFormat('Ymd', get_field('other_dates'));
    $otherdates2 = DateTime::createFromFormat('Ymd', get_field('other_dates_2'));
    $otherdates3 = DateTime::createFromFormat('Ymd', get_field('other_dates_3'));
    $today = DateTime::createFromFormat('Ymd', date('Ymd'));

/* I need to set up the first available variable here */    
$comparedate = $otherdates3 || $otherdates2 || $otherdates1 || $maindate;

if ( $comparedate >= $today ) : 

/* list the future events */

How can I make this comparison?

Related posts

2 comments

  1. $lastestDate = null;
    $datesSet = [$maindate, $otherdates1, $otherdates2, $otherdates3];
    foreach($datesSet as $date) {
         if($lastestDate == null || $lastestDate->getTimeStamp() < $date->getTimeStamp())
              $lastestDate = $date;
    }
    $comparedate = $lastestDate;
    
  2. As pointed by Ianis, I had to compare the variables by php max operator, and the if statement gets the latest date like that.

    The variable is:

    $comparedate = max( $maindate, $otherdates1, $otherdates2, $otherdates3 );
    

Comments are closed.