MySQL DateTime NOW() as the default value

Hey there,
need some help with setting my default value in a table to the current datetime
for some reason i cant get the NOW() function to work if i use the DATETIME type.
however, when i use TIMESTAMP type and CURRENT_TIMESTAMP it work!

any ideas how can i get the DATETIME type working?
thanks ahead,
Dan
Capture.PNG
maatayimAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Cornelia YoderArtistCommented:
A mysql function cannot be used as a default.  You will need to use TIMESTAMP field or enter NOW() as part of the values in an INSERT.
0
maatayimAuthor Commented:
hi there,
correct me if i'm wrong but timestamp has a differnt way of recording time.
from what i understood the time is being kept in utc and when i retrive is from the data base is converts it back to my timezone.
this could be problematic if u work with servers world wide and try to keep track of dates and times...

is there a ways of using it and still read the real UTC time?
thanks
Dan
0
Cornelia YoderArtistCommented:
To convert any date requires you know the time zone of the end user.  That means client-side code, and there are javascript functions that can do that for you.  But it would still be stored in the database as UTC.

http://www.fruitnotes.com/blogs/Javascript_-_converting_unix_timestamp_to_locat_time_format_758

0
SoLostCommented:
Firstly, it is inadvisable to have your field named DateTime as DATETIME is a Datatype.  It can potentially cause you headaches.

As previously stated, you can't have a mysql function as the default.

If you can't add NOW() to your MySQL statements then I would look into adding a TRIGGER to your table.

First, set the default value of DateTime in your table to be NULL

While I haven't actually tried this, something like the following may work :
DELIMITER $$

CREATE TRIGGER default_date BEFORE INSERT ON tbl
    FOR EACH ROW BEGIN
        IF NEW.DateTime IS NULL THEN
            SET NEW.DateTime=NOW();
        END IF;
    END;
$$

DELIMITER ;

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
MySQL Server

From novice to tech pro — start learning today.