【问题标题】:HTML5 Datetime-local Displays 01-01-1970 01:00 When its emptyHTML5 Datetime-local 显示 01-01-1970 01:00 当它为空时
【发布时间】:2015-08-17 15:22:12
【问题描述】:

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

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

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

下面是html5和php的代码sn-p。

<?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');
?>

【问题讨论】:

  • 你的问题是?
  • 这是 unix 时代的开始(可能加上 DST)。只需检查该字段是否为空并相应输出即可。

标签: php mysql html datetime


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-04-12
    • 1970-01-01
    • 1970-01-01
    • 2016-06-03
    • 2012-03-14
    • 1970-01-01
    • 1970-01-01
    • 2018-05-05
    相关资源
    最近更新 更多