将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.
原文:https://stackoverflow.com/questions/6817231
更新时间: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
相关问答
使用strtotime(..) : $timestamp = strtotime($mysqltime);
echo date("Y-m-d H:i:s", $timestamp);
还要检查一下(以MySQL的方式做) http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_unix-timestamp Use strtotime(..): $timestamp = strtotime($mysq
...
如果月份等于十月(十个月),则选择表格events所有列。 SELECT * FROM events WHERE MONTH(FROM_UNIXTIME(event_date)) = 10
更多的时间和日期相关的功能可以在MySQL手册中找到 Selects all columns from table events if the month equals October (10th month). SELECT * FROM events WHERE MONTH(FROM_UNIXTIME(e
...
2之间存在5.5小时的差异 ,这表明它是两个时区问题。 可以以不同方式配置MySql Server的时区。 SET time_zone = timezonename;
可用于为当前会话设置时区。 检查MySQL日期和时间函数 There is a 5.5 hour difference between the 2, which indicates it is a timezone issue, on either end. The MySql Server's Timezone could be
...
strtotime()用于将各种日期格式的字符串转换为UNIX时间戳。 如果字符串已经是时间戳,则它看起来不像strtotime()的有意义的格式化日期/时间,它将失败。 如果字符串已经是一个时间戳,那么你不需要再做任何事情而不是将其强制转换为整数(严格来说,即使该步骤也不是必需的,但是强制转换会删除任何非数字字符,所以它通常不会伤害演员) 另外,MySQL能够解析UNIX时间戳(使用FROM_UNIXTIME(),我想,我必须查找它以确定) strtotime () is intended fo
...
strtotime函数将假定datetime在服务器的本地时区。 The strtotime function will assume the datetime is in the server's local timezone.
这被证实是5.1.44中修复的错误。 有关详细信息,请参阅http://bugs.mysql.com/bug.php?id=51918 ,错误海报完全找到了此问题。 你将别无选择,只能升级以避免它的外观。 This is confirmed to be a bug that is fixed in 5.1.44. See http://bugs.mysql.com/bug.php?id=51918 for details, the bug poster has found this issue e
...
如果数据库中的timestamp列的类型为INTEGER,则可以执行此操作 mysql_query("INSERT INTO permissions (date) VALUES ('".time()."')");
作为整数值,您还可以执行排序操作,并通过PHP中的date()函数将其转换为可读的日期/时间格式。 如果数据库中的时间戳列是DATETIME类型,那么可以这样做 mysql_query("INSERT INTO permissions (date) VALUES ('".date('Y
...
TIMESTAMP是一个非常直接的选择。 它在内部实现为UNIX时间戳,但在外部它显示为日期字符串(例如“1970-01-01 00:00:01”)。 编辑:将INT迁移到时间戳,其中time_test是表, ts是原始列: ALTER TABLE time_test ADD ts_new TIMESTAMP;
UPDATE time_test SET ts_new = FROM_UNIXTIME(ts);
ALTER TABLE time_test DROP ts;
ALTER TABLE ti
...
$variable = time();
应该做你想要的。 $variable = time();
should do what you're looking for.