1

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 = "";
            }
Tylee
  • 153
  • 5
  • 2
    For the date formatting request, take a look at https://www.php.net/manual/en/function.date.php. Also you should take a look at prepared SQL Queries (https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) to be safe against SQL Injection. – klediooo May 24 '22 at 06:39
  • 1
    fyi, the `` element in deprecated, use CSS instead. And `2px` is not very much for displaying text – brombeer May 24 '22 at 06:42
  • 1
    You can start with if conditions. – nice_dev May 24 '22 at 06:45
  • 1
    @nice_dev I've updated the codes. I tried with if condition but still not changing the color. – Tylee May 24 '22 at 06:51
  • **Warning:** You are wide open to [SQL Injections](https://php.net/manual/en/security.database.sql-injection.php) and should use parameterized **prepared statements** instead of manually building your queries. They are provided by [PDO](https://php.net/manual/pdo.prepared-statements.php) or by [MySQLi](https://php.net/manual/mysqli.quickstart.prepared-statements.php). Never trust any kind of input! Even when your queries are executed only by trusted users, [you are still in risk of corrupting your data](http://bobby-tables.com/). [Escaping is not enough!](https://stackoverflow.com/q/32391315) – Dharman May 24 '22 at 09:41

2 Answers2

1
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 >";
}
nice_dev
  • 14,216
  • 2
  • 21
  • 33
  • It change the color but it did not show under EWEMJ002 and also did not show the time. – Tylee May 24 '22 at 07:18
  • 1
    @Tylee I edited to show the time as well. I have no idea what `EWEMJ002` is. I presume you can simply extract the color logic instead of the entire code then. – nice_dev May 24 '22 at 07:19
  • U can check in the screenshot. Now color changes and date time also appears but it has to show in the table as in the screenshot. Currently, it's showing above the table. – Tylee May 24 '22 at 07:24
  • 1
    @Tylee I just changed `p` tag to `font` tag if that helps. You will have to show full code if you wish a fix for that as well. – nice_dev May 24 '22 at 07:26
  • 1
    It works now. I changed to $date="
    " instead of echo"". It works for me.
    – Tylee May 24 '22 at 07:35
  • ALso, is there anyway to show date count in above code? – Tylee May 25 '22 at 02:19
  • @Tylee You might want to post this as a new question. – nice_dev May 25 '22 at 03:46
-1
$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 = "";
}