将php文件中的unix时间戳存储到mysql中(store unix timestamp from php file into mysql)

现在我有这个代码:

$mysqldate = date(time());
mysql_query("INSERT INTO permissions (date)
VALUES ('$mysqldate')");

我也插入之前逃脱,但我的问题是,时间被存储为全0。 我想知道mysql中的列数据类型会存储类似于:

1311602030

一个unix时间戳,然后将正确地允许我按查询中的最近日期排序。

Right now I have this code:
$mysqldate = date(time());
mysql_query("INSERT INTO permissions (date)
VALUES ('$mysqldate')");
I escape before I insert also, but my problem is that the time is getting stored as all 0s. I was wondering what column datatype in mysql would store something like:
1311602030
a unix timestamp, and then would properly allow me to order by most recent dates on a query.

更新时间:2020-03-03 11:29

最满意答案

如果数据库中的timestamp列的类型为INTEGER,则可以执行此操作

mysql_query("INSERT INTO permissions (date) VALUES ('".time()."')");

作为整数值,您还可以执行排序操作,并通过PHP中的date()函数将其转换为可读的日期/时间格式。 如果数据库中的时间戳列是DATETIME类型,那么可以这样做

mysql_query("INSERT INTO permissions (date) VALUES ('".date('Y-m-d H:i:s')."')");

要么

mysql_query("INSERT INTO permissions (date) VALUES (NOW())");
If timestamp column in database is of type INTEGER you can do
mysql_query("INSERT INTO permissions (date) VALUES ('".time()."')");
As integer value you can also do sort operation and convert it via date() function from PHP back to a readable date/time format. If timestamp column in database is of type DATETIME you can do
mysql_query("INSERT INTO permissions (date) VALUES ('".date('Y-m-d H:i:s')."')");
or
mysql_query("INSERT INTO permissions (date) VALUES (NOW())");
2011-07-25