的config.php
@$mysqli= mysqli_connect($servername, $username_bd, $password_bd, $dbname);
if($mysqli->connect_error)
return false;
insert.php
include 'config.php';
$DateDefault = '2015-07-30 00:00:00
2015-08-30 00:00:00';
$sql = "INSERT INTO users (date) VALUES ('$DateDefault')";
if($query = $mysqli->query($sql)===true)
echo "Date add successfully";
else
echo "Error into date";
show.php
include 'config.php';
$sql = "SELECT date FROM users WHERE username='$username'";
$query = $mysqli->query($sql);
if (@$query->num_rows > 0){
while ($dados = $query->fetch_assoc()) {
echo substr($dados['date'], 21, 19);
}
}
结果: " 2015-08-30 00:00:00"
现在,当我在phpmyadmin中更改日期时,
"2015-07-30 00:00:00
2015-09-30 00:00:00"
显示:
"015-09-30 00:00:00 "
在localhost中,使用wampserver,这个bug不会发生
答案 0 :(得分:0)
您的问题可能与how different operating systems treat line endings有关。简而言之,在Windows上,行结尾占用两个字符(回车符和换行符,\r\n
),而在大多数其他操作系统上,它们只占用一个(换行符,\n
)。通过使用特定的字符偏移,您可以引入您所看到的逐个错误的可能性。
一个简单的解决方法是使用单个空格(或其他字符)而不是换行符来分隔日期。如果您仍想使用换行符,可以split the string by whitespace:
$dates = preg_split('/\s+/', $dados['date']); // Split dates by whitespace
echo $dates[1]; // Print the second date