dsshsta97935 2019-04-11 12:21
浏览 38

需要检查日期时间并在我的div中添加一个类

 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;

i think i almost got it but still not working perfect if i dump the class its giving me

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条回答 默认 最新

  • drbii0359 2019-04-11 12:59
    关注

    Create two DateTime objects, and compare them in your conditions. DateTime objects can be compared to see which is the biggest (in date-time).

    Preferably you would have one column date instead of separated into two different columns.

    $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';
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)