0

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);
    }
}

Result: "2015-08-30 00:00:00"

Now, when I changed the date in the phpmyadmin, for

"2015-07-30 00:00:00
2015-09-30 00:00:00"

Shows this:

"015-09-30 00:00:00 "

In localhost, using wampserver, this bug don't happen

HaveNoDisplayName
  • 8,291
  • 106
  • 37
  • 47

1 Answers1

0

Your issue is likely to do with how different operating systems treat line endings. In a nutshell, on Windows, line endings take up two characters (carriage return and newline, \r\n), and on most other operating systems, they occupy only one (newline, \n). By using specific character offsets, you're introducing the possibility of the off-by-one error you're seeing.

A simple fix would be to separate your dates by using a single space (or another character) instead of a line break. If you still want to use a line break, you can split the string by whitespace:

$dates = preg_split('/\s+/', $dados['date']); // Split dates by whitespace
echo $dates[1]; // Print the second date
Community
  • 1
  • 1
Jacob Budin
  • 9,753
  • 4
  • 32
  • 35
  • My localhost is running in Windows and the server in Linux. Each OS work in a different way with line endings. I choose your second solution, because is more easy see the date in phpmyadmin with line endings when I need update manually. thanks for resolve my problem @Jacob budin – user2022323 Jul 31 '15 at 01:27