I am working on a PHP - MySQL application where I have a requirement to convert all stored non date values to NULL in a DATE column and in future avoid insertion of 0000-00-00 values to column.In short a DATE column should only have either valid DATE values or NULL.Suppose I have a table with a column type as DATE and default value to NULL.When I tried to insert data into these column with a non date value, instead of setting the value to NULL the column is set as '0000-00-00'.So if I have the date value as an empty string or a non valid date string I don't want the value '0000-00-00' to get inserted.I am using a MySQLi wrapper class library to connect PHP application with MySQL database.The link is given below.
https://github.com/ThingEngineer/PHP-MySQLi-Database-Class
It is difficult for me to check at each insert/update operation in the application where the date column value is indeed a date value and if not, unset the column or set value of column to NULL.So I tried to do something at the place where the insert/update command used by the library (All insert/update commands pass through this library).But there also I should have some predefined information on what tables and what columns are DATE type (Probably has to fetch table meta information from information Schema table) at hand to compare and act accordingly.
I have found some similar functionality already in JDBC library like in the thread (Please see the answer with 5 upvotes).
https://dba.stackexchange.com/questions/139896/null-instead-0000-00-00-000000-in-mysql-5-7
So is there anything similar like zeroDateTimeBehaviour = convertTONull option in JDBC Driver that we can make use of in PHP - MYSQL connection interface or some conversion thing at DB level.
I am using PHP 5.6 and MySQL Version 5.6.
Edit - Many pointed out the question already has an answer in
MySQL, how to insert null dates
But all the answers do not lead to correct solution (not in my case at least) as many answers explains how to tackle problem at each insert/update level but not when the application already has a lot of insert/update commands and I want to modify the current application in least possible effort.I am looking to something like
zeroDateTimeBehaviour = convertTONull option in JDBC Driver