我的代码从数据库中提取日期。如果返回的日期为NULL,我希望它显示为00-00-0000或Nothing而不是1970年1月1日。 这是我的代码:
<?php
while($row=mysql_fetch_array($res))
{
$date_paid="";
if ($date_paid == NULL) {
$date_paid = $row['date_paid'];
} else{
$date_paid = date("jS M, Y", strtotime($row['date_paid']));
}
echo "<tr><td>$date_paid</td></tr>";
}
?>
答案 0 :(得分:1)
使用MySQL IF
或DATE_FORMAT
Select *,
IF(date_paid IS NULL, '00-00-0000', DATE_FORMAT(NOW(),'%D %b, %Y'))
date_paid From table
或其他选项将此==
更改为此,因为===
比较类型或值
if ($date_paid === NULL) {
答案 1 :(得分:0)
简单的解决方案......
while($row = mysql_fetch_array($res)){
if(empty($row['date_paid']) || $row['date_paid']==='0000-00-00'){
$row['date_paid'] = '00-00-0000';
} else {
$row['date_paid'] = date("jS M, Y", strtotime($row['date_paid']));
}
echo $row['date_paid'];
}
或内联..
while($row = mysql_fetch_array($res)) {
$row['date_paid'] = empty($row['date_paid']) ? '00-00-0000' : date("jS M, Y", strtotime($row['date_paid']));
echo $row['date_paid'];
}
答案 2 :(得分:0)
尽可能简单:
$date_paid = $row['date_paid']
? date("jS M, Y", strtotime($row['date_paid']))
: '00-00-0000';