简体   繁体   English

用PHP将当前时间戳插入MySQL?

[英]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. 我已经在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 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? 这是一个整数值(根据我读,自1970年1月1日的秒数,例如1338949763.但是当我尝试插入的东西,如使用INSERT INTO entries (date) VALUES (NOW())NOW东西会存储格式化的日期,例如2012-04-12 23:09:54 。我不想要那个。我想存储整数值,该怎么办?

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

尝试使用类似"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; 如果你想插入时间戳,即一些整数值到MySQL字段; 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()."');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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