我有一个数据库行,其类型为“timestamp”.每次添加新记录时,它会自动填充该记录插入的时间.现在,对于其中一个记录,它保存了值:
2010-06-19 18:40:51
我尝试通过以下方式将其转换为更加用户友好的格式:
$timeStamp = $row['date'];
$timeStamp = date( "m/d/Y", $timeStamp);
但它返回这个:
12/31/1969
我在某处读过日期函数只能在unix时间戳上使用,也许我在数据库中的内容不是一个合适的时间戳,我需要先将它转换为一个才能使用日期函数(?).解决办法是什么?
解决方法:
$timeStamp = $row['date'];
$timeStamp = date( "m/d/Y", strtotime($timeStamp));
对于您的示例2010-06-19 18:40:51中的日期,它将返回:
06/19/2010
时间戳字段会自动更新,但您可以通过将其更改为DEFAULT CURRENT_TIMESTAMP来禁用该行为,否则它将自动更新.从manual开始:
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.
标签:php,mysql
来源: https://codeday.me/bug/20190526/1158659.html