Jump to content

[SOLVED] How to remove, or not display, the 0000-00-00 in date fields?


sleepyw

Recommended Posts

Total noob question. :(

 

My understanding is that, by default, a date field in MySQL will populate 0000-00-00 if nothing is entered.

 

My question is, can I change that in MySQL to leave that area blank if there is no value? If not, is there an easy way to code PHP to not show a 0000-00-00 without writing an if statement for every date field where this applies?

 

TIA.

Link to comment
Share on other sites

Yes, you should default it to NULL. Make sure the field can contain a NULL. Only defaults to 0000-00-00 if you insert an empty value or an invalid date

 

<?php
$created = "";
mysql_query("INSERT INTO table SET created=".(strlen($created) ? "'".$created."'" : "NULL")."");
?>

Link to comment
Share on other sites

EDIT: to test, I erased the 000-00-00 value in a few fields and set to NULL and it appears to work.

 

However, I would need/want to run UPDATE command to make the change to the db globally. Something like:

 

UPDATE table
SET %all fields that apply% = 'NULL'
WHERE %all fields that apply% = '0000-00-00'

 

The text between the %'s would need to be a catch-all for the whole db. Is that possible? I don't understand what your code does.

Link to comment
Share on other sites

No - I'm saying instead of listing out every column name where it might apply, how do I do a global"search and replace" from 0000-00-00 to NULL?

 

I can't use the field name "whatever".  Could I use SET * = 'NULL' WHERE * = '0000-00-00'?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.