A
A
Anton Dyshkant2015-04-25 01:05:25
PHP
Anton Dyshkant, 2015-04-25 01:05:25

How to store date by default in MySQL or why DateTime('0000-00-00') shows nonsense?

Hello!
The question is very philosophical. Base, table, the table has optional date fields.
For convenience, I made them default-values ​​(0000-00-00), so that in PHP both filled and unfilled fields are treated equally (display zeros or real dates).
What was my surprise when, as a result of executing this code

$date = new DateTime('0000-00-00');
echo $date->format("Y-m-d");

instead of the expected zeros, I saw "-0001-11-30".
As I managed to find out, this is not a bug, but a PHP feature.
Accordingly, I would like to know how to store an optional MySQL date more competently? Should I use NULL or some other mask?
Z.Y. Storage with a default value of '0000-00-00' I have seen in some CMS.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
sivabur, 2015-04-25
@vyshkant

Use NULL.

S
Sanes, 2015-04-25
@Sanes

Unix Timestamp is probably the most racially correct format.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question