尝试应用PHP日期格式

时间:2015-03-17 00:54:35

标签: php mysql date

我正在尝试从MySql应用某种日期格式。列字段类型为' date'并在MySQL中格式化为2015-03-16的示例,那些没有日期的字段是 NULL

当我应用以下代码时,带有日期的字段将正确显示 - 2015年3月16日。

但是,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";

1 个答案:

答案 0 :(得分:0)

那么你必须确定缺少的日期,而不是把其他东西拿出去,就像这样

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";
}