需要检查日期时间并将类添加到我的div中

时间:2019-04-11 12:21:35

标签: php

 foreach($this->connection->query($sql) as $row) {

                     $class = "";
                     if($row['datum'] > date("Y-m-d") && $row['tijd'] > date("H:i:s")) { $class = "toekomst"; }
                     if($row['datum'] == date("Y-m-d") && $row['tijd'] == date("H:i:s")) { $class = "vandaag"; }
                     if($row['datum'] < date("Y-m-d") && $row['tijd'] < date("H:i:s")) { $class = "verleden"; }

                     var_dump($class);
                     $output .= "<tr class='$class'>";
                          $output .= "<td>";
                               //Rotate date from sql to dutch notation
                               $datum   = $row['datum'];
                               $dag     = substr($datum, 8, 2);
                               $maand   = substr($datum, 5, 2);
                               $jaar    = substr($datum, 0, 4);
                               $datum   = $dag . "-" . $maand . "-" . $jaar;

                               $output .= $datum;

我想我差不多可以做到,但是如果我放弃给我的课程

string(0) "" string(8) "verleden" string(0) "" string(0) "" string(0) "" string(0) "" string(0) "" string(0) "" string(0) "" string(0) "" string(8) "verleden" string(0) "" string(0) "" string(8) "toekomst" string(8) "toekomst"

2 个答案:

答案 0 :(得分:0)

创建两个DateTime对象,并在您的条件下进行比较。可以比较DateTime对象以查看最大的对象(以日期时间为单位)。

最好有一个列date,而不是分成两个不同的列。

$now = new DateTime();
$date = new DateTime($row['datum']." ".$row['tijd']);

if ($now > $date) {
    $class = 'toekomst';
} elseif ($now == $date) {
    $class = 'vandaag';
} elseif ($now < $date) {
    $class = 'verleden';
}

答案 1 :(得分:0)

if($row['datum'] > date("Y-m-d")) { $class = "toekomst"; }
if($row['datum'] == date("Y-m-d")) {
    if($row['tijd'] > date("H-i-s")) {$class = "vandaag"; }
else{ $class = "verleden"; }}