如果输出等于00:00:00,如何更改回声输出

时间:2015-12-26 21:47:01

标签: php mysql css datetime

如果时间戳等于0000-00-00 00:00:00我希望我的网站输出“未知”我该怎么做?这是我的代码:

<h1>Exabit URL Shortening Service</h1>
<?php
    $con=mysqli_connect("localhost","***","***","***");
    $result = mysqli_query($con,"SELECT * FROM links");

    while($row = mysqli_fetch_array($result))
      {
      echo "<p>"; echo "ID: "; echo $row['id']; echo " URL: "; echo $row['url']; echo " Created on: "; echo $row['created']; 
      echo "</p>";
      }
    mysqli_close($con);
?>
<link href='https://fonts.googleapis.com/css?family=Open+Sans:300' rel='stylesheet' type='text/css'>
<title>Exabit URL Shortener Database</title>
<style>
body {
background-color: black;
}
p {
padding:2px;
border:1px solid white;
font-family:'Open Sans', sans-serif;
color:white;
text-align: center;
right: 8px;
left: 8px;
}
h1 {
color:white;
font-family:'Open Sans', sans-serif;
text-align:center;
}
</style>

提前致谢, 扎克。

2 个答案:

答案 0 :(得分:2)

由于$ row [&#39; created&#39;]应该是一个字符串,只需检查一下:

if ($row['created'] == "0000-00-00 00:00:00"){
    echo "Unknown";
}
else {
    echo $row['created'];
}

将你的代码放在一行,你可以做到这一点

echo ($row['created'] == "0000-00-00 00:00:00") ? 'Unknown' : $row['created'];

答案 1 :(得分:1)

我认为如果返回时间戳我认为这是它检查的方式

if ($row['created'] > 0) {
    echo $row['created'];
} else {
    echo 'UnKnown';
}

或内联IF

echo ($row['created'] == 0) ? 'Unknown' : $row['created'];