我正在尝试从MySql应用某种日期格式。列字段类型为"date",在 MySQL 中格式化,例如 2015-03-16,那些没有日期的字段为 NULL。
当我应用以下代码时,带有日期的字段会正确显示 - 03/16/2015。
但是,MySQL 中的所有 NULL 字段都显示在网页中,显示 31/12/69。知道我能做什么吗?
if(!$rs = mysql_query("SELECT Vessel_Name, Builder, Built, Listing_Price, Date_Listed, LOA, Price_Original, Price_Previous, Amt_Reduced, Amt_Pct FROM boats")) {
echo "Cannot parse query";
}
elseif(mysql_num_rows($rs) == 0) {
echo "No records found";
}
else {
echo "<table id='"boatstable'" class='"bordered'" cellspacing='"0'">'n";
echo "<thead>'n<tr>";
echo "<th>Vessel_Name</th>";
echo "<th>Builder</th>";
echo "<th>Built</th>";
echo "<th>Listing Price</th>";
echo "<th>Date Listed</th>";
echo "<th>LOA</th>";
echo "<th>Original Price</th>";
echo "<th>Previous Price</th>";
echo "<th>Amt Reduced</th>";
echo "<th>Amt Pct</th>";
echo "</tr>'n</thead>'n";
while($row = mysql_fetch_array($rs)) {
echo "<tr><td>$row[Vessel_Name]</td><td>$row[Builder]</td><td>$row[Built]</td><td>$row[Listing_Price]</td><td>".date("m/d/y", strtotime($row['Date_Listed']))."</td><td>$row[LOA]</td><td>$row[Price_Reduced]</td><td>$row[Price_Previous]</td><td>$row[Amt_Reduced]</td><td>$row[Amt_Pct]</td></tr>'n";
}
echo "</table><br />'n";
好吧,您将不得不确定缺少的日期并放出其他内容,例如
while($row = mysql_fetch_array($rs)) {
$fixed_date = $row['Date_Listed'] == 'NULL'
? 'n/a'
: date("m/d/y", strtotime($row['Date_Listed']));
echo "<tr>
<td>$row[Vessel_Name]</td>
<td>$row[Builder]</td>
<td>$row[Built]</td>
<td>$row[Listing_Price]</td>
<td>". $fixed_date ."</td>
<td>$row[LOA]</td>
<td>$row[Price_Reduced]</td>
<td>$row[Price_Previous]</td>
<td>$row[Amt_Reduced]</td>
<td>$row[Amt_Pct]</td>
</tr>'n";
}