default date of today for table

A

Anonymous

Guest
Sorry if this has already been asked at one point, but I didn't see anything when I searched for it.

I created a table with a date field. How do I give that date field a default date of today when a record is created? I want to do this as a default value rather then having to pass it in when I populate the table.

thanks
 
gesf said:
Code:
mysql> CREATE TABLE test (mydate DATE);
mysql> INSERT INTO test SET mydate=NOW();

Shouldn't that be:
Code:
INSERT INTO test (..., date, ...) VALUES (..., NOW(), ...);
 
Good deal. Thanks for the reply's.

I figured I would have to do it with the insert or update and pass it in as a field value, which I knew how to do. I mainly wanted a date field with a default date value of the current date so I wouldn't have to pass one in when creating/updating the record.

I found that if using the date or time field types, you can only have constant default values, so I can't use a time function as the default. I did find, and ended up using, the TIMESTAMP field type. If you have a column of type TIMESTAMP and then do not pass in a value, it will get assigned the current system time/date. This is mainly what I wanted, and it seems to work good.

Thanks
http://www.mysql.com/doc/en/DATETIME.html
 
Skeletor said:
I did find, and ended up using, the TIMESTAMP field type. If you have a column of type TIMESTAMP and then do not pass in a value, it will get assigned the current system time/date.

Ack, how did I forget that? Good find.
 
the way that i do it is i make a hidden form field with the name of "date" and the value of a phpdate line. then in my query i do $_POST['date'].

ahem...

Code:
<input type="hidden" name="date" value="<?echo date("F j, Y, g:i a");?>">

that date shows up Month DD YYYY 00:00 AM. and the obvious....


Code:
INSERT INTO "table" (date) VALUES ('".$_POST['date']."');


D$ in full effect
 
Yeh, for a formated date and time. It´s a nice idea!
This is another way:
Code:
<?
$datetime = date("Y-m-d H:i:s");
mysql_query("INSERT INTO table (date) VALUES ($datetime);");
?>
 
Back
Top