如何从 PHP 中的时间戳格式化日期?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3080921/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to format date from timestamp in PHP?
提问by James
I have a database row whose type is "timestamp". It automatically populates each time a new record has been added with the time of that record's insertion. Right now for one of the records it holds the value:
我有一个类型为“时间戳”的数据库行。每次添加新记录时,它都会自动填充该记录的插入时间。现在,对于其中一项记录,它保存的值:
2010-06-19 18:40:51
I tried to turn this in to a more user friendly format by:
我试图通过以下方式将其转换为更用户友好的格式:
$timeStamp = $row['date'];
$timeStamp = date( "m/d/Y", $timeStamp);
But it returns this:
但它返回这个:
12/31/1969
I read somewhere that the date function can only be used on unix timestamps, perhaps what I have in my database isn't a proper timestamp and I need to convert it to one first before using the date function on it (?). What is the solution?
我在某处读到 date 函数只能在 unix 时间戳上使用,也许我在数据库中的时间戳不是正确的时间戳,我需要先将其转换为 1,然后才能在其上使用 date 函数(?)。解决办法是什么?
回答by Sarfraz
Use strtotimefunction:
使用strtotime函数:
$timeStamp = $row['date'];
$timeStamp = date( "m/d/Y", strtotime($timeStamp));
For the date from your example 2010-06-19 18:40:51, it would return:
对于您示例中的日期2010-06-19 18:40:51,它将返回:
06/19/2010
The timestampfield updates automatically but you can disable that behavior by changing it to DEFAULT CURRENT_TIMESTAMPotherwise it will auto update. From the manual:
该timestamp字段DEFAULT CURRENT_TIMESTAMP会自动更新,但您可以通过将其更改为禁用该行为,否则它将自动更新。从手册:
In a CREATE TABLE statement, the first TIMESTAMP column can be declared in any of the following ways:
With both DEFAULT CURRENT_TIMESTAMP and ON UPDATE CURRENT_TIMESTAMP clauses, the column has the current timestamp for its default value, and is automatically updated.
With neither DEFAULT nor ON UPDATE clauses, it is the same as DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP.
With a DEFAULT CURRENT_TIMESTAMP clause and no ON UPDATE clause, the column has the current timestamp for its default value but is not automatically updated.
在 CREATE TABLE 语句中,第一个 TIMESTAMP 列可以通过以下任何一种方式声明:
使用 DEFAULT CURRENT_TIMESTAMP 和 ON UPDATE CURRENT_TIMESTAMP 子句,该列的默认值具有当前时间戳,并且会自动更新。
既没有 DEFAULT 也没有 ON UPDATE 子句,它与 DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP 相同。
使用 DEFAULT CURRENT_TIMESTAMP 子句而没有 ON UPDATE 子句时,列的默认值具有当前时间戳,但不会自动更新。
回答by Aaron Yodaiken
strtotime($timeStamp)(string to time) is what you want to convert into a unix timestamp.
strtotime($timeStamp)(string to time) 是您想要转换为 unix 时间戳的内容。
Then you can use the date on that, so something like date( "m/d/Y", strtotime($timeStamp) )
然后你可以使用日期,所以像 date( "m/d/Y", strtotime($timeStamp) )
回答by cypher
SELECT UNIX_TIMESTAMP(sql_row_with_timestamp) AS some_much_more_readable_name FROM your_tableshould do the trick.
SELECT UNIX_TIMESTAMP(sql_row_with_timestamp) AS some_much_more_readable_name FROM your_table应该做的伎俩。

