php怎么将时间戳格式化,如何格式化PHP中的时间戳日期?

我有一个数据库行,其类型为“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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值