有没有一种简单的方法从PHP中的SQL时间戳获取Unix时间戳?

时间:2022-01-10 14:54:23

My database table has a column that contains SQL timestamps (eg. 2009-05-30 19:43:41). I need the Unix timestamp equivalent (an integer) in my php program.

我的数据库表有一个包含SQL时间戳的列(例如,2009-05-30 19:43:41)。我需要在我的php程序中使用Unix时间戳等效(一个整数)。

$posts = mysql_query("SELECT * FROM Posts ORDER BY Created DESC");
$array = mysql_fetch_array($posts);
echo $array[Created];

Where it now echoes the SQL timestamp, I want a Unix timestamp. Is there an easy way to do this?

它现在回应SQL时间戳,我想要一个Unix时间戳。是否有捷径可寻?

2 个解决方案

#1


strtotime() will happily take a SQL timestamp and convert it to UNIX time:

strtotime()会愉快地获取SQL时间戳并将其转换为UNIX时间:

echo strtotime($array['Created']);

Note that it is bad practice to not use quotes around your array keys. According to the docs:

请注意,不要在数组键周围使用引号是不好的做法。根据文件:

Always use quotes around a string literal array index. For example, $foo['bar'] is correct, while $foo[bar] is not.

始终在字符串文字数组索引周围使用引号。例如,$ foo ['bar']是正确的,而$ foo [bar]则不是。

This is wrong, but it works. The reason is that this code has an undefined constant (bar) rather than a string ('bar' - notice the quotes). PHP may in future define constants which, unfortunately for such code, have the same name. It works because PHP automatically converts a bare string (an unquoted string which does not correspond to any known symbol) into a string which contains the bare string. For instance, if there is no defined constant named bar, then PHP will substitute in the string 'bar' and use that.

这是错误的,但它确实有效。原因是这段代码有一个未定义的常量(bar)而不是一个字符串('bar' - 注意引号)。 PHP可能在将来定义常量,不幸的是,这些代码具有相同的名称。它的工作原理是因为PHP自动将一个裸字符串(一个与任何已知符号不对应的不带引号的字符串)转换为包含裸字符串的字符串。例如,如果没有定义的名为bar的常量,那么PHP将替换字符串'bar'并使用它。

#2


Given a regular datetime field, you can do this in query with the MySQL function UNIX_TIMESTAMP():

给定一个常规日期时间字段,您可以使用MySQL函数UNIX_TIMESTAMP()进行查询:

$posts = mysql_query("SELECT *
                             , UNIX_TIMESTAMP(Created)
                      FROM     Posts
                      ORDER BY Created DESC");

you also should know that the PHP function mysql_fetch_array returns both numeric associative keys for a query result. I find this redundant, so I like to be more more specific and use:

您还应该知道PHP函数mysql_fetch_array返回查询结果的两个数字关联键。我发现这是多余的,所以我更喜欢更具体和使用:

$array = mysql_fetch_array($posts, MYSQL_ASSOC);

Also, and as @Paolo Bergantino indicated, quoting your echo would be a good decision:

此外,正如@Paolo Bergantino指出的那样,引用你的回声将是一个很好的决定:

echo $array['Created'];

One last comment, you're using "SELECT *" -- it is worth reading discussion on the pros and cons of "SELECT *" syntax.

最后一条评论,您正在使用“SELECT *” - 值得阅读有关“SELECT *”语法的优缺点的讨论。

#1


strtotime() will happily take a SQL timestamp and convert it to UNIX time:

strtotime()会愉快地获取SQL时间戳并将其转换为UNIX时间:

echo strtotime($array['Created']);

Note that it is bad practice to not use quotes around your array keys. According to the docs:

请注意,不要在数组键周围使用引号是不好的做法。根据文件:

Always use quotes around a string literal array index. For example, $foo['bar'] is correct, while $foo[bar] is not.

始终在字符串文字数组索引周围使用引号。例如,$ foo ['bar']是正确的,而$ foo [bar]则不是。

This is wrong, but it works. The reason is that this code has an undefined constant (bar) rather than a string ('bar' - notice the quotes). PHP may in future define constants which, unfortunately for such code, have the same name. It works because PHP automatically converts a bare string (an unquoted string which does not correspond to any known symbol) into a string which contains the bare string. For instance, if there is no defined constant named bar, then PHP will substitute in the string 'bar' and use that.

这是错误的,但它确实有效。原因是这段代码有一个未定义的常量(bar)而不是一个字符串('bar' - 注意引号)。 PHP可能在将来定义常量,不幸的是,这些代码具有相同的名称。它的工作原理是因为PHP自动将一个裸字符串(一个与任何已知符号不对应的不带引号的字符串)转换为包含裸字符串的字符串。例如,如果没有定义的名为bar的常量,那么PHP将替换字符串'bar'并使用它。

#2


Given a regular datetime field, you can do this in query with the MySQL function UNIX_TIMESTAMP():

给定一个常规日期时间字段,您可以使用MySQL函数UNIX_TIMESTAMP()进行查询:

$posts = mysql_query("SELECT *
                             , UNIX_TIMESTAMP(Created)
                      FROM     Posts
                      ORDER BY Created DESC");

you also should know that the PHP function mysql_fetch_array returns both numeric associative keys for a query result. I find this redundant, so I like to be more more specific and use:

您还应该知道PHP函数mysql_fetch_array返回查询结果的两个数字关联键。我发现这是多余的,所以我更喜欢更具体和使用:

$array = mysql_fetch_array($posts, MYSQL_ASSOC);

Also, and as @Paolo Bergantino indicated, quoting your echo would be a good decision:

此外,正如@Paolo Bergantino指出的那样,引用你的回声将是一个很好的决定:

echo $array['Created'];

One last comment, you're using "SELECT *" -- it is worth reading discussion on the pros and cons of "SELECT *" syntax.

最后一条评论,您正在使用“SELECT *” - 值得阅读有关“SELECT *”语法的优缺点的讨论。