php - HTML5 日期时间本地显示 01-01-1970 01 :00 When its empty

标签 php mysql html datetime

我正在构建一个Web应用程序,并使用html5 datetime-local mysql来存储数据,并使用php从数据库中获取datea。日期数据类型与 sql datetime 一起存储,不为 null,也没有默认值。

但是当数据通过html5 datetime-local标签显示时,当日期时间字段为空时,它在浏览器中显示01-01-1970 01:00。

我的问题是当字段为空时我想显示 00-00-0000 00:00。

下面是 html5 和 php 的代码片段。

<?PHP

session_start();

if (!(isset($_SESSION['login_user']) && $_SESSION['login_user'] != '')) {

header ("Location: loginForm.php");

}

?>


<?php
include('/templates/header.php');
$host = "localhost"; // Host name 
$username = "root"; // Mysql username 
$password = ""; // Mysql password 
$db_name = "datacentre"; // Database name 
$tbl_name = "data_centre_users"; // Table name 
$server_name = "localhost";

// Create connection
$con = new mysqli($server_name, $username, $password, $db_name, 3306);
if($con->connect_error){
   die("Connection failed: ".$con->connect_error);
}

// Check connection
if($con->connect_error){
 die("Connection failed: ".$conn->connect_error);
}

$sql = "SELECT * FROM admin";
$result = $con->query($sql);


function myDate($x){

  return strftime('%Y-%m-%dT%H:%M:%S',
               strtotime($x));

}         


?>

<section id="sidebar">

</section>

<section id="content">

<div id="scroll-table">
<table >
<caption>

            </caption>
            <tr>
                <th class="center"><strong>ID</strong></th>
                <th class="center"><strong>User Name</strong></th>
                <th class="center"><strong>Time Created</strong></th>
            </tr>
            <?php
            if($result->num_rows > 0){
                // output data of each row
                while($rows = $result->fetch_assoc()){ ?>
                    <tr>
                        <td class="center"><?php echo $rows['id']; ?></td>
                        <td class="center"><?php echo $rows['user_name']; ?></td>  
                        <td class="center">
                         <input name="time_created" disabled="disabled" type="datetime-local" id="time_created" value="<?php echo myDate($rows                         ['time_created']); ?>" size="15">
                        </td>
                    </tr>

                    <?php
                }
            }
            ?> 
</table>
</div>
</section>


<aside></aside>

<?php
$con->close();

include('/templates/footer.php');
?>

最佳答案

当向其传递无效值时,

strtotime() 返回 false。当您尝试格式化 false 时,您会得到 Unix 纪元,即 1970 年 1 月 1 日。

如果您不希望该值出现,请检查 $x 是否不包含任何值,并返回空字符串或其他默认值。

关于php - HTML5 日期时间本地显示 01-01-1970 01 :00 When its empty,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32054069/

相关文章:

html - 带有叠加层的图像上的超链接

php - 如果在查询中不考虑用户输入,使用 mysqli_query() 是否危险?

javascript - WordPress:在 function.php 中排队后,javascript 未加载

MySql 错误 1064 - 使用 MySQL WorkBench 创建

mysql - sql内连接删除

PHP 2人从同一台电脑同时登录pt.02

java - 使用 Jsoup 选择没有类的 HTML 元素

html - 如何在 4 列中显示我的自定义 woocommerce 产品?

php - PHP如何处理重复的URL参数?

php - 选择-仅显示一次值