Reputation: 3819
am building a web application and am using the html5 datetime-local mysql to store the data and php to fetch datea from the database. The date datatype is stored with the sql datetime , not null and no default value.
But when the data is displayed by the html5 datetime-local tag , it displays 01-01-1970 01:00 in the browser when the datetime field is empty.
My question is I want to display 00-00-0000 00:00 when the field is empty.
Below is a code snippet for the html5 and 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');
?>
Upvotes: 0
Views: 1747
Reputation: 219884
strtotime()
returns false
when an invalid value is passed to it. When you try to format false
you get the Unix Epoch which is Jan 1, 1970.
If you do not want that value to appear check to see if $x
contains no value and either return an empty string or some other default value.
Upvotes: 2