想要根据日期改变颜色
Want to make color change depending on date
我想根据日期改变颜色。比方说,如果小于今天的日期,则必须显示为红色,否则必须显示为绿色color.I 将提供屏幕截图。
如截图所示,2022-05-25 必须为绿色,2021-10-16 必须为红色。另外,我想按顺序显示 DD-MM-YY。我可以知道我该怎么做吗?
{
$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 = "";
}
$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 = "";
}
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>";
}
代替上述脚本,使用 DateTime
class 对象而不是字符串比较来比较日期时间总是更好。
您可以像下面这样修改您的代码:
<?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 >";
}
我想根据日期改变颜色。比方说,如果小于今天的日期,则必须显示为红色,否则必须显示为绿色color.I 将提供屏幕截图。
如截图所示,2022-05-25 必须为绿色,2021-10-16 必须为红色。另外,我想按顺序显示 DD-MM-YY。我可以知道我该怎么做吗?
{
$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 = "";
}
$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 = "";
}
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>";
}
代替上述脚本,使用 DateTime
class 对象而不是字符串比较来比较日期时间总是更好。
您可以像下面这样修改您的代码:
<?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 >";
}