Page 1 of 1

How do I set MySQL column to current date on insert only?

Posted: Wed Feb 05, 2003 3:36 pm
by 3dron
Where do I apply this:

TIMESTAMP columns other than the first may also be set to the current date and time. Just set the column to NULL or to NOW().

If I set the column to NULL or put NOW() in the default value then date comes up 0000-00-00 upon record insert.

If I put a hidden field and submit 0000-00-00 I get same result.

So how do I just get date once upon record creation?

Thanks

RR

try this

Posted: Thu Feb 06, 2003 8:40 pm
by phpScott
I just spent a bunch of time playing with datefields and the like and the only way i could consistenly get it to work right was by doing this.

INSERT INTO someTable (name, address, date) VALUES($name, $address, date_format(NOW(), "%Y%m%d"));

this will explicitly set the NOW value that is being generated and make sure that it will be set into the db as 2003/02/06 or something like that.

phpScott

Posted: Fri Feb 07, 2003 7:39 pm
by 3dron
Ok, I guess I'm stupid.

I can't get it to work. Can it be done with a date field?
I am using your exact form of

date_format(NOW(), "%Y%m%d")

right in the date VALUE (between the commas with a space)

Ah, help!!!

ooops

Posted: Sat Feb 08, 2003 2:53 am
by phpScott
sorry, try this
INSERT INTO someTable (name, address, date) VALUES($name, $address, CURRENT_DATE());

phpScott