Page 1 of 1

Current date in mysql

Posted: Thu Nov 25, 2010 3:53 am
by klevis miho
What property should I give a table column to record the date today.
I added timestamp, and it displays me: 2010-11-25 02:57:30

I want just the date.

Re: Current date in mysql

Posted: Thu Nov 25, 2010 4:35 am
by social_experiment
What about DATE

Re: Current date in mysql

Posted: Thu Nov 25, 2010 4:51 am
by klevis miho
Like:
ALTER TABLE tableName ADD today_date CURDATE() ?

This won't work.

Re: Current date in mysql

Posted: Thu Nov 25, 2010 4:55 am
by klevis miho
I solved it but not in the database level.
I did it like this.

ALTER TABLE tableName ADD today_date DATE;

Then in php:

$query1 = mysql_query("INSERT INTO tableName(today_date) VALUES(NOW())");

But I want it in the database level.

Re: Current date in mysql

Posted: Thu Nov 25, 2010 7:43 am
by internet-solution
[syntax]INSERT INTO tableName (today_date) VALUES (CURDATE())[/syntax]

Re: Current date in mysql

Posted: Thu Nov 25, 2010 7:49 am
by klevis miho
Thanks, but I wanted something that if I do not insert anything in the db, the default will be the current date.

Re: Current date in mysql

Posted: Thu Nov 25, 2010 8:14 am
by internet-solution
w3school says it can be done - see here - http://www.w3schools.com/sql/func_curdate.asp

But when I try their code on my server (MySQL 5.0.45), it shows an error. It only accepts current_timestamp as default value and this only works if field type is timestamp. (Edit - confirmation here - http://dev.mysql.com/doc/refman/5.0/en/ ... aults.html)

Is there a particular reason you want to add only the date?

Re: Current date in mysql

Posted: Thu Nov 25, 2010 8:17 am
by klevis miho
hehehe the same happens to me, I get an error :) .
I want only the date, because I don't need the time.
But anyways I got it working.

Thanks.