简体   繁体   中英

How to input the current time into a mysql table from PHP

I'm using a mysql_query() in PHP to insert a value into a table in a database. One of the attributes of that table is "timestamp" which is supposed to be the current time that the query was made. How can I generate this? I tried using "time()" in PHP, but this is not inserting correctly in the query. When I insert what the time() function generates, it shows up as a default in my table, as: "0000-00-00 00:00:00". How can I generate the current time in order to input it into my table?

In your query, you need to set the value of that field to NOW() which is MySQL's function for getting a timestamp of the current time.

If you put your actual query in your question, I can help you with a more specific answer ;)

insert into table (datetime_field) values (now())

php函数time()返回一个unix时间戳,它是一个整数值。

Go to php.net and view the codes for getting date and time and check whether you typed the codes correctly. If you did, then check if the format you are using in your code is similar to the one you are using in your table.

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