php 使用php将当前时间戳插入mysql?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10134979/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
insert current timestamp into mysql with php?
提问by Rafael Adamy
This is driving me crazy. I've searched it on interent but can't get to a conclusion.
这真让我抓狂。我已经在interent上搜索过,但无法得出结论。
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 NOWthing 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?
问题是我前段时间做过,我不记得我是如何将这些值插入到该字段中的。它是一个整数值(根据我读到的内容,自 1970 年 1 月 1 日以来的秒数,例如 1338949763。但是当我尝试插入内容时,例如使用INSERT INTO entries (date) VALUES (NOW())。该NOW东西将存储格式化的日期,例如2012-04-12 23:09:54。我不想要那个. 我想存储整数值。我该怎么做?
回答by leepowers
Use MySQL's UNIX_TIMESTAMP()function to get the current epoch time:
使用 MySQL 的UNIX_TIMESTAMP()函数获取当前纪元时间:
INSERT INTO mytable SET tstamp = UNIX_TIMESTAMP();
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_unix-timestamp
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_unix-timestamp
回答by wyqydsyq
Try using something like "INSERT INTO entries (date) VALUES ('".time()."')"
尝试使用类似的东西 "INSERT INTO entries (date) VALUES ('".time()."')"
回答by Cassie Smith
The correct function is:
正确的函数是:
getdate()
NOT
不是
NOW()
回答by user473445
if u want to insert timestamp i.e. 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
如果你想在 mysql 字段中插入时间戳,即一些整数值;字段类型应该是“时间戳”而不是“日期时间”然后我希望你的整数值会被数据库接受
回答by 4302836
you may try this for insert
你可以试试这个插入
INSERT into Table_name (colunm_name) VALUES ('".time()."');

