【问题标题】:PHP-MYSQL Display different colors according to a deadlinePHP-MYSQL 根据期限显示不同的颜色
【发布时间】:2012-07-02 05:01:15
【问题描述】:

如何根据mysql数据库数据中建立的截止日期显示不同的div背景颜色?

即 mysql日期:10-10-2012

display background-color: #FF9999 如果今天是从“之前”到 1-10-2012
display background-color: #FF0000 如果今天是从 1-10-2012 到 10-10-2012
display background-color: #FFFFFF 如果今天是从2012 年 10 月 10 日到“之后”

谢谢

【问题讨论】:

  • 显示更多代码可以帮助可以帮助您的人

标签: php mysql date time colors


【解决方案1】:

例如(未经测试):

在您的样式表中定义:

.before { background-color: #FF9999; } 
.current { background-color: #FF0000; } 
.after { background-color: #FFFFFF; }

在你的 php 中:

$iNow = time();
$iDeadline = strtotime($sMySqlDate);
$iAfter = strtotime('+1 day', $iDeadline);
$iBefore = strtotime('-10 days', $iDeadline);
$sClass = ($iNow >= $iAfter ? 'after' : ($iNow < $iBefore ? 'before' : 'current'));
echo '<div class="' . $sClass . '">...</div>';

=== 更新 ===

从 mysql 数据库中读取时间戳:

$sSql = "SELECT `closedate` FROM `table`";
$rResult = mysql_query($sSql);
if (!$rResult) {
    echo "Could not successfully run query ($sSql) from DB: " . mysql_error();
    exit;
}

$aRow = mysql_fetch_assoc($rResult);
$sMySqlDate = $aRow['closedate'];
mysql_free_result($rResult);

【讨论】:

  • 太棒了!如何从 MySQL 数据库($sMySqlDate 或 $iDeadline)“检索”数据?
  • 你能先给我你的数据库结构(表名和列)吗?
  • 我的表是:ID - listname - opendate - closedate
【解决方案2】:

从数据库中获取当前日期的时间戳和日期的时间戳,然后进行比较。使用 strtotime 函数。

例子:

$date1 = strtotime('01-07-2012');
$date2 = strtotime('01-02-2009');

if($date1 > $date2) echo 'The first date was before second one.';

【讨论】:

  • 你可以简单地在行首插入4个空格(空格键),整行将被解析为<pre>。如果我没记错的话,它之前应该还有一个空的新行。</pre>
【解决方案3】:

您可以使用date()strtotime() 的组合。在我的示例中,我假设您在某种 SQL 查询结果中有日期,例如$result['date']2012-10-10 而不是 10-10-2012

$date = strtotime($result['date']);
$before = strtotime('2012-01-01');
$after = strtotime('2012-12-31');

$today = strtotime(date(Y-m-d));

if($today >= $before && $today < strtotime('2012-10-01')){
    echo 'display background-color: #FF9999';
}
elseif($today >= strtotime('2012-10-01') && $today <= strtotime('2012-10-10')){
    echo 'display background-color: #FF0000';
}
elseif($today > strtotime('2012-10-10') && $today <= $after){
    echo 'display background-color: #FFFFFF';
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-07
    • 2018-05-20
    • 1970-01-01
    • 1970-01-01
    • 2016-02-24
    • 1970-01-01
    相关资源
    最近更新 更多