PHP strtotime store false as MySQL negative int

I am doing a transition and dates starting as "0000-00-00 00:00:00" and going through strtotime () and returning as false are stored as -2147483648 in a new table column which is an INT datatype. whether I set the default to 0 or NULL, it retains the value of that negative number. I want to avoid having to code exceptions for each date field (and I have no way to change the datatype), so is there an easy way in PHP (using one line) or MySQL to set the default to these "empty" dates?

+3


source to share


3 answers


Time 0000-00-00

is out of range strtotime

, which is limited by 1901-12-13 to 2038-01-19. It returns the bottom border of your broken data. This is the case for almost all UNIX-based functions time_t

.



My advice is to replace these dates with NULL

before processing. They are not valid.

+1


source


It's hard to answer without code, but you can use the Terminal Operator .

ex:

$variable_to_write_to_db = ($field == 0 || $field == NULL ? writeDefaultsToDB() : writeToDB($field))

      

Or something like that.



The ternary operator works like this (copied from the link above):

/* most basic usage */
$var = 5;
$var_is_greater_than_two = ($var > 2 ? true : false); // returns true

      

Good luck, have fun

+2


source


Are you looking for a default value for a column and just don't pass the date to it (i.e. just set all dates to some value if no value is specified?)

If so, if you want the default to be 0 (which I'm not sure if you would, would it be better to allow null if dates are nonsense?):

ALTER TABLE YourTable MODIFY YourColumn Default '0';

      

Then make sure you are not missing anything (or null should be okay) to the database when called with an unsuccessful result strtotime

(the note strtotime

will not return valid results for dates before Fri 13 Dec 1901 at 20:45:54 UTC, so your 0000-00-00 would be wrong on this).

0


source







All Articles