Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prevent PHP to convert a DateTime object with 0000-00-00 value to -0001-11-30

Tags:

php

datetime

Currently i'm working with Doctrine 2 and MySQL. I encounter some problems when I work with datetime fields in mysql respectively DateTime in PHP. When in my database the date value is "0000-00-00", in PHP this value is converted to -0001-11-30. I'm note pleased about that, so I need to make a check for "0000-00-00" value of date. Does anybody have some idea on this to help? Thanks.

N.B. I'm thinking if it is right to make checking on "-0001-11-30" instead of "0000-00-00".

like image 519
artaxerxe Avatar asked Jul 06 '11 06:07

artaxerxe


People also ask

How do you handle null values in a date column?

The DATE and TIMESTAMP functions do exactly what you are looking for. If you have a STRING column where its format is like TIMESTAMP , you can simply apply it. Then, DATE will extract just the date and it takes care of the NULL values.

Can a date field be null?

If the type isn`t specified, Flexmonster automatically assigns a type for a certain field according to a first value. Empty string and null values can not be recognized as dates, as the result date formatting does not work.

What is zeroDateTimeBehavior?

zeroDateTimeBehavior=convertToNull. --- See MySQL Manual: --- Connector/J x.x throws an exception by default when these values are encountered as this is the most correct behavior according to the JDBC and SQL standards.

What is datetime FSP?

DATETIME[( fsp )] A date and time combination. The supported range is '1000-01-01 00:00:00.000000' to '9999-12-31 23:59:59.999999' . MySQL displays DATETIME values in ' YYYY-MM-DD hh:mm:ss [. fraction ]' format, but permits assignment of values to DATETIME columns using either strings or numbers.


1 Answers

If the date is not set use NULL to indicate that state. That solves your problem and makes database schema much more clear and verbose.

like image 188
Crozin Avatar answered Oct 10 '22 02:10

Crozin