DATE Auto Generate

Posted on 2006-05-07
Medium Priority
Last Modified: 2008-02-01
hi guys,
in mySQL, when i insert new stuff, i have a row setup for date, how can i get it to automatically gets the time/date of the submission and ad it to the field?
so lets say, right now is 2 pm and i just added a new row, how can it automatically find out what the time and date is, and automatically ads it to the date row? is it possible?
Question by:djsoltan
LVL 21

Expert Comment

by:Julian Matz
ID: 16627823

Do you use PHP to insert data? If so, you could do:

$date = date('Y-m-d');
mysql_query("INSERT INTO table SET current_date='$date'", $connection);

Expert Comment

ID: 16628724
I assume that you want the time and date of the machine running the database server to be added.

Suppose you have a table created as

create table t1 (
   id INTEGER,
   submission DATETIME

Then you can use

insert into table values (1,NOW());

Author Comment

ID: 16630770
hi,,, thanks for the inputs,
i am using phpmyadmin to insert into the table,
i tried the
insert into table values (1,NOW());
it works if i run the command line to do the insert,
but if i want to use the 'insert' page and add it manually,
the date always comes back as
0000-00-00 00:00:00
any idea on why it is doing this?
i am using NOW() in the field,
am i doing smth wrong?
Veeam and MySQL: How to Perform Backup & Recovery

MySQL and the MariaDB variant are among the most used databases in Linux environments, and many critical applications support their data on them. Watch this recorded webinar to find out how Veeam Backup & Replication allows you to get consistent backups of MySQL databases.


Author Comment

ID: 16630881
what can i set my date row default to, so that it automatically ads it on every insert without having to do it manualy?
LVL 11

Accepted Solution

star_trek earned 1000 total points
ID: 16631115
change the dafualt value to NOW() on that timestamp column for that table.
alter table <table-name> change <date-column-name> <date-column-name> timestamp default NOW();

Author Comment

ID: 16631216
still the same,,, i do alter the table and set the default to NOW() but it automatically places the 0000-00-00 00:00:00 in there again,,, i guess NOW() is a MYSqL Function and thats why it doesnt take it... :-s
any idea experts on how to do this? :-s
LVL 11

Expert Comment

ID: 16631329
MySQL should take the function NOW() for the default values. What version of MySQL you are using? Is the datatype for that column is timestamp?

Author Comment

ID: 16631383
i got it, i had datetime instead of timestamp :D
thanks alot :D

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
In this blog post, we’ll look at how ClickHouse performs in a general analytical workload using the star schema benchmark test.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses
Course of the Month13 days, 8 hours left to enroll

750 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question