php有效期交通灯系统
我正在研究交通灯系统。它有点乱,没有按应有的方式工作 我的目标是让系统像这样工作 数据库中有供应商保险到期的日期。如果到期日期是30天或更短,那么我希望这些日期被拖过,并被呼出,但其他在30天之后到期的日期不应显示 目前,它也给出了截止日期的天数,但这并不正确,所以说今天是5月2日,明天是3日;保险单据将于3日到期,所以应该说保险明天到期。这是目前无法正常工作,我得到的是像'2天'到期,而不是'明天到期' 像怀斯一样,如果保险今天到期,它应该说今天到期,但它没有得到正确的日期 此外,我还设计了不同颜色的div标签,根据保险文件的到期日期显示为一种交通灯信号。如果文档将在30到20天内到期,我希望显示绿色div;如果文档将在19到7天内到期,我希望它显示为琥珀色;如果文档将在7到0天内到期或过期,则它应显示为红色 这是我的代码,请有人告诉我,我可以如何改进它做我需要它做的事,谢谢 代码:php有效期交通灯系统,php,mysql,css,Php,Mysql,Css,我正在研究交通灯系统。它有点乱,没有按应有的方式工作 我的目标是让系统像这样工作 数据库中有供应商保险到期的日期。如果到期日期是30天或更短,那么我希望这些日期被拖过,并被呼出,但其他在30天之后到期的日期不应显示 目前,它也给出了截止日期的天数,但这并不正确,所以说今天是5月2日,明天是3日;保险单据将于3日到期,所以应该说保险明天到期。这是目前无法正常工作,我得到的是像'2天'到期,而不是'明天到期' 像怀斯一样,如果保险今天到期,它应该说今天到期,但它没有得到正确的日期 此外,我还设计了不
更改查询以准确反映到期日前的天数,这样可以简化检查。下面的查询截短为天,并以正数形式给出了到期前的准确天数 正数是几天前的事。所以+1意味着明天结束 零意味着今天结束 负数表示它已过期
SELECT *, TIMESTAMPDIFF(DAY, CURRENT_DATE(), DATE(insurance_date)) AS days_before_expires FROM supplier_stats
已转换为“mysqli”
所有代码都已经过测试。它实现“红绿灯”状态。我已经更改了格式代码,试图通过使用函数计算“紧急指示器”使其更易于维护。现在所有的格式化都是使用css完成的
<!DOCTYPE HTML">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF8">
<meta name="generator" content="PSPad editor, www.pspad.com">
<title>23424062/php-expiry-date-traffic-light-system</title>
<style type="text/css">
table {
width:995px;
font-family: 'Lucida Grande', Tahoma, Verdana, Arial, sans-serif;
font-size:11px;
}
th {text-align: left; }
.width_small {
width: 100px;
}
.width_medium {
width: 150px;
}
.width_large {
width: 200px;
}
.urgency_1 {
display: inline-block;
width: 6em;
background-color: green;
color: yellow;
padding-left: 1em;
}
.urgency_2 {
display: inline-block;
width: 6em;
background-color: orange;
padding-left: 1em;
}
.urgency_3 {
display: inline-block;
width: 6em;
background-color: red;
color: yellow;
padding-left: 1em;
}
.green_light {
width: 3em;
background-color: green;
}
.amber_light {
width: 3em;
background-color: orange;
}
.red_light {
width: 3em;
background-color: red;
}
</style>
</head>
<body>
<?php
$mysqli = new mysqli('localhost', 'test', 'test', 'testmysql');
$data = $mysqli->query("SELECT *, "
." TIMESTAMPDIFF(DAY, CURRENT_DATE(), DATE(insurance_date)) AS days_before_expires, "
." DATE(insurance_date) as insurance_day "
." FROM supplier_stats order by DATE(insurance_date)")
or die(mysql_error());
?>
<table>
<tr><th class="width_small">ID:</th><th>Company Name:</th><th>Company Reg No:</th><th>Owner:</th><th class="width_large">Note:</th><th class="width_small">Date:</th><th>Status:</th></tr>
<?php while($row = $data->fetch_array()): ?>
<?php $daysLeft = $row['days_before_expires']; ?>
<?php $urgency = getUrgency($daysLeft); ?>
<?php $cssTrafficLight = selectUrgencyLit($row['days_before_expires'],
array('', 'green_light', 'amber_light', 'red_light')); ?>
<?php $cssUrgency = selectUrgencyLit($row['days_before_expires'],
array('urgency_0', 'urgency_1', 'urgency_2', 'urgency_3')); ?>
<?php $daysLeftLit = getDaysLeftLiteral($daysLeft); ?>
<tr><td class="width_small"><p><?= $row['id'] ?></p></td>
<td class="width_medium"><p><?= $row['company_name'] ?></p></td>
<td class="width_medium"><p><?= $row['company_reg_number'] ?></p></td>
<td class="width_small"><p><?= $row['owner'] ?></p></td>
<td class="width_large"><?= $daysLeftLit; ?></td>
<td class="width_small"><?= date('d/m/Y',strtotime($row['insurance_day'])) ?></td>
<td class="width_medium"><div class="<?= $cssTrafficLight?>"> </div></td>
<tr>
<?php endwhile; ?>
</table> <!-- Close the table in HTML -->
</body>
</html>
<?php
/*
* To tidy the code up and hopefully make it easier to maintain we need
* a couple of functions...
*
* How about assigning an 'urgency level' to each record depending on the
* days left before the expiry date. The level is as follows:
*
* 0 => not urgent (more than 20 days)
* 1 => need to pay attention (14 - 20)
* 2 => soon (7 - 13)
* 3 => now (0 - 6)
*/
function getUrgency($daysLeft)
{
if ($daysLeft >= 21 || $daysLeft < 0) { return 0; }
if ($daysLeft >= 14 && $daysLeft <= 20) { return 1; }
if ($daysLeft >= 7 && $daysLeft <= 13) { return 2; }
return 3;
}
// return a literal depending on the urgency
function selectUrgencyLit($daysLeft, array $litArray)
{
$urgency = getUrgency($daysLeft);
if (!empty($litArray[$urgency])) {
return $litArray[$urgency];
}
return '';
}
// return the days left literal
function getDaysLeftLiteral($daysLeft)
{
$urgency = getUrgency($daysLeft);
if ($urgency <= 0) {
return ' ';
}
$text = "Insurance Expires: <div class=\"urgency_{$urgency}\">";
// set the day literal to be shown
if ($daysLeft == 0) { $dayLit = 'today'; }
elseif ($daysLeft == 1) { $dayLit = 'tomorrow'; }
elseif ($daysLeft >= 0 && $daysLeft <= 20) { $dayLit = "in {$daysLeft} days"; }
else { $dayLit = ' '; }
return $text . $dayLit .'</div>';
}
<!DOCTYPE HTML">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF8">
<meta name="generator" content="PSPad editor, www.pspad.com">
<title>23424062/php-expiry-date-traffic-light-system</title>
<style type="text/css">
table {
width:995px;
font-family: 'Lucida Grande', Tahoma, Verdana, Arial, sans-serif;
font-size:11px;
}
th {text-align: left; }
.width_small {
width: 100px;
}
.width_medium {
width: 150px;
}
.width_large {
width: 200px;
}
.urgency_1 {
display: inline-block;
width: 6em;
background-color: green;
color: yellow;
padding-left: 1em;
}
.urgency_2 {
display: inline-block;
width: 6em;
background-color: orange;
padding-left: 1em;
}
.urgency_3 {
display: inline-block;
width: 6em;
background-color: red;
color: yellow;
padding-left: 1em;
}
.green_light {
width: 3em;
background-color: green;
}
.amber_light {
width: 3em;
background-color: orange;
}
.red_light {
width: 3em;
background-color: red;
}
</style>
</head>
<body>
<?php
$mysqli = new mysqli('localhost', 'test', 'test', 'testmysql');
$data = $mysqli->query("SELECT *, "
." TIMESTAMPDIFF(DAY, CURRENT_DATE(), DATE(insurance_date)) AS days_before_expires, "
." DATE(insurance_date) as insurance_day "
." FROM supplier_stats order by DATE(insurance_date)")
or die(mysql_error());
?>
<table>
<tr><th class="width_small">ID:</th><th>Company Name:</th><th>Company Reg No:</th><th>Owner:</th><th class="width_large">Note:</th><th class="width_small">Date:</th><th>Status:</th></tr>
<?php while($row = $data->fetch_array()): ?>
<?php $daysLeft = $row['days_before_expires']; ?>
<?php $urgency = getUrgency($daysLeft); ?>
<?php $cssTrafficLight = selectUrgencyLit($row['days_before_expires'],
array('', 'green_light', 'amber_light', 'red_light')); ?>
<?php $cssUrgency = selectUrgencyLit($row['days_before_expires'],
array('urgency_0', 'urgency_1', 'urgency_2', 'urgency_3')); ?>
<?php $daysLeftLit = getDaysLeftLiteral($daysLeft); ?>
<tr><td class="width_small"><p><?= $row['id'] ?></p></td>
<td class="width_medium"><p><?= $row['company_name'] ?></p></td>
<td class="width_medium"><p><?= $row['company_reg_number'] ?></p></td>
<td class="width_small"><p><?= $row['owner'] ?></p></td>
<td class="width_large"><?= $daysLeftLit; ?></td>
<td class="width_small"><?= date('d/m/Y',strtotime($row['insurance_day'])) ?></td>
<td class="width_medium"><div class="<?= $cssTrafficLight?>"> </div></td>
<tr>
<?php endwhile; ?>
</table> <!-- Close the table in HTML -->
</body>
</html>
<?php
/*
* To tidy the code up and hopefully make it easier to maintain we need
* a couple of functions...
*
* How about assigning an 'urgency level' to each record depending on the
* days left before the expiry date. The level is as follows:
*
* 0 => not urgent (more than 20 days)
* 1 => need to pay attention (14 - 20)
* 2 => soon (7 - 13)
* 3 => now (0 - 6)
*/
function getUrgency($daysLeft)
{
if ($daysLeft >= 21 || $daysLeft < 0) { return 0; }
if ($daysLeft >= 14 && $daysLeft <= 20) { return 1; }
if ($daysLeft >= 7 && $daysLeft <= 13) { return 2; }
return 3;
}
// return a literal depending on the urgency
function selectUrgencyLit($daysLeft, array $litArray)
{
$urgency = getUrgency($daysLeft);
if (!empty($litArray[$urgency])) {
return $litArray[$urgency];
}
return '';
}
// return the days left literal
function getDaysLeftLiteral($daysLeft)
{
$urgency = getUrgency($daysLeft);
if ($urgency <= 0) {
return ' ';
}
$text = "Insurance Expires: <div class=\"urgency_{$urgency}\">";
// set the day literal to be shown
if ($daysLeft == 0) { $dayLit = 'today'; }
elseif ($daysLeft == 1) { $dayLit = 'tomorrow'; }
elseif ($daysLeft >= 0 && $daysLeft <= 20) { $dayLit = "in {$daysLeft} days"; }
else { $dayLit = ' '; }
return $text . $dayLit .'</div>';
}