简体   繁体   中英

insert current timestamp into mysql with php?

This is driving me crazy. I've searched it on interent but can't get to a conclusion.

I have a date field in my database. Which I'm retrieving values with date function just fine.

Problem is I did it some time ago, and I don't remember how I inserted those values into that field. it's an integer value (according to what I read, the number of seconds since jan 1st 1970, for example 1338949763. but when I try inserting stuff, like using INSERT INTO entries (date) VALUES (NOW()) . The NOW thing will store a formatted date, like 2012-04-12 23:09:54 . I don't want that. I want to store the integer value. how do I do that?

Use MySQL's UNIX_TIMESTAMP() function to get the current epoch time:

INSERT INTO mytable SET tstamp = UNIX_TIMESTAMP();

http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_unix-timestamp

尝试使用类似"INSERT INTO entries (date) VALUES ('".time()."')"

The correct function is:

getdate()

NOT

NOW()

if u want to insert timestamp ie some integer value into mysql field; the field type should be "timestamp" not "datetime" then I hope your integer value will be accepted by the DB

您可以尝试此插入

INSERT into Table_name (colunm_name) VALUES ('".time()."');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM