Home > Software engineering >  Want to make color change depending on date
Want to make color change depending on date

Time:05-24

I want to make color changing depends on date. Let's say, if less than today's date, it must show with red color otherwise it must show with green color.I will provide the screenshot.

2022-05-25 must be green and 2021-10-16 must be red.

As u can check in the screenshot, 2022-05-25 must be green and 2021-10-16 must be red. Also, I want to show DD-MM-YY in order. May I know how can I do that?

{
            $type = $equipArray[$x];
            
            $sql= "SELECT * FROM map_db_oee.`tbl_actual_correlation` WHERE `waferid` LIKE 'NSX1234%' AND prober='".$type."' ORDER BY dt_corr DESC LIMIT 1";
            //echo $sql.'<br>';
            $result = mysqli_query($conn, $sql);
            if (mysqli_num_rows($result) > 0) {
                $row = mysqli_fetch_array($result);
                if ($date < $now ){
                    $date = "<br><font size=\"2px\" color=\"red\">".$row["dt_corr"]."</font>";}
                else {
                    $date = "<br><font size=\"2px\" color=\"green\">".$row["dt_corr"]."</font>";
                }
                
                //echo $date.'<br>';
            }
            else{
                $date = "";
            }

CodePudding user response:

if ($date < $now ){
    $date = "<br><font size=\"2px\" color=\"red\">".$row["dt_corr"]."</font>";}
else {
    $date = "<br><font size=\"2px\" color=\"green\">".$row["dt_corr"]."</font>";
}

Instead of the above script, it is always better to compare datetimes using DateTime class objects instead of string comparisons. You can modify your code like below:

<?php

if (mysqli_num_rows($result) > 0) {
    $row = mysqli_fetch_array($result);
    $curr_dt = DateTime::createFromFormat("Y-m-d H:i:s", date("Y-m-d") . " 00:00:00");

    $row_dt = DateTime::createFromFormat("Y-m-d H:i:s", $row["dt_corr"]);

    $color = $row_dt < $curr_dt ? 'red' : 'green';

    echo "<br><font  style='color: $color; font-size:12px'>".date("d-m-Y H:i:s",strtotime($row["dt_corr"]))."</font >";
}

CodePudding user response:

$type = $equipArray[$x];
        
$sql= "SELECT * FROM map_db_oee.`tbl_actual_correlation` WHERE `waferid` LIKE 'NSX1234%' AND prober='".$type."' ORDER BY dt_corr DESC LIMIT 1";

$result = mysqli_query($conn, $sql);

if (mysqli_num_rows($result) > 0) {

    $row = mysqli_fetch_array($result);

    if($row["dt_corr"] < date("Y/m/d H:i:s")){
        $date = "<br><p style="color:red; font-size:12px">".$row["dt_corr"]."</p>";
    }elseif($row["dt_corr"] > date("Y/m/d H:i:s")){
        $date = "<br><p style="color:green; font-size:12px">".$row["dt_corr"]."</p>";
    }
}else{
    $date = "";
}
  • Related