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

3
  • The date column is two dates?
    – chris85
    Jul 31, 2015 at 0:07
  • Thx for your response, the date column is 'utf8_unicode_ci' Jul 31, 2015 at 1:06
  • Not charset. Actually data in the column is two dates? Column must not be a datetime field, maybe varchar or text?
    – chris85
    Jul 31, 2015 at 1:13

1 Answer 1

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
1
  • 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 Jul 31, 2015 at 1:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.