database icon indicating copy to clipboard operation
database copied to clipboard

Date from mysql is wrong converted to Nette/Utils/DateTime

Open MichalKalita opened this issue 8 years ago • 2 comments

  • bug report? yes
  • feature request? no
  • version: 2.4.2

Description

In my application, I read date from database, and write it to other table. Date "0000-00-00 00:00:00" is bad converted to php, and cannot be saved back to mysql.

Exception: SQLSTATE[22007]: Invalid datetime format: 1292 Incorrect datetime value: '-0001-11-30 00:00:00' for column 'date' at row 1

Steps To Reproduce

In database I have date "0000-00-00 00:00:00", data type is datetime not null. InnoDB. I use simple NDBT select

$row = $this->table(..)->wherePrimary(...)->fetch();
echo $row->date; // prints "-0001-11-30 00:00:00"
// and write it back
$this->table(...)->wherePrimary(...)->update(['date' => $row->date]); // it throws exception, mysql cannot save this date

PHP 7.1.2, mysql 5.6.22, I use InnoDB for all tables.

MichalKalita avatar May 19 '17 20:05 MichalKalita

0000-00-00 00:00:00 is not a valid datetime; make to column nullable. such value may cause problems also directly in the mysql.

hrach avatar May 19 '17 22:05 hrach

For interest Mysql 5.7 does not allow invalid datetime "0000-00-00 00:00:00". Let's prepare your databese :) Like @hrach recommended you.

h4kuna avatar Jun 30 '17 05:06 h4kuna