Search code examples
phpmysqldate

mysql date shows 30/11/-000 instead of 0000-00-00


I know this is already asked but I can't find a correct answer also because my problem is a little different.

I have a web app written in php with mysql. Everything works fine over the past years until some days ago I moved the app to a new much performing server, the source and the db. Everything run smoothly but not in this date case.

The wrong date is saved on a mysql table with 0000-00-00 but when I show it appears 30/11/-000

How to get rid off this? I still have the old working server so I can check for mysql or php configuration. I think the problem is simple as a configuration parameter, but I can't find the right one.  

This is the snippet for retreiving the date. This is not my code, and can't ask to the original developer, can you explain me why there is a check if the date is 30/11/1999? Seems a common problem google says, and I think is involved.

    function getDataF($data){

       if (($stadata = strtotime($data)) == -1 || $stadata === false) {
           return "";
       }

       $return = date("d/m/Y",$stadata);

       if ($return == "30/11/1999") return "";

       return $return;

}

Solution

  • strtotime returns 30/11/1999 when the input is 00-00-00 00:00:00. Code Pad here. This explains why developer checked if the date is 30/11/1999 and return empty in that case.

    Regarding why 0000-00-00 dates displayed as 31/11/-000: It seems there is a bug (or "feature") in strtotime function that it returns -0001 as year for some PHP versions (example here). So, for your invalid dates, date results in 31/11/-0001. I guess the code that display dates strips one character from the end as it expects year as 4 digits.