DATE Auto Generate

Posted on 2006-05-07
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

    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);
    LVL 1

    Expert Comment

    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

    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?

    Author Comment

    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

    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

    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

    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

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

    Featured Post

    Find Ransomware Secrets With All-Source Analysis

    Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

    Join & Write a Comment

    Both Easy and Powerful How easy is PHP? (  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
    Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL ( several years ago, it seemed like now was a good time to updat…
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

    731 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

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now