比较变量并设置第一个有值的变量

时间:2016-02-18 09:18:00

标签: php wordpress

我有一个Event lister循环,我从Event的日期和今天的日期创建了变量。有一个必需的$ maindate,但是还有其他日期相同的事件$ otherdates,晚于主要日期。 我想设置在单独变量中设置的最新日期,并将其与今天的日期进行比较,以使if($ compareed> = $ today)仅控制列表中的未来事件。

以下是我的变数:

SELECT DISTINCT Key, 
FIRST_VALUE(Value) OVER (PARTITION BY Key ORDER BY SubKey DESC)
FROM TableA

我该如何进行比较?

2 个答案:

答案 0 :(得分:1)

$lastestDate = null;
$datesSet = [$maindate, $otherdates1, $otherdates2, $otherdates3];
foreach($datesSet as $date) {
     if($lastestDate == null || $lastestDate->getTimeStamp() < $date->getTimeStamp())
          $lastestDate = $date;
}
$comparedate = $lastestDate;

答案 1 :(得分:0)

正如Ianis指出的那样,我必须通过php max运算符来比较变量,而if语句会得到最新的日期。

变量是:

$comparedate = max( $maindate, $otherdates1, $otherdates2, $otherdates3 );
相关问题