gpt4 book ai didi

php 日期函数返回一个我没想到的结果

转载 作者:行者123 更新时间:2023-11-29 01:30:31 25 4
gpt4 key购买 nike

在我的 mysql 数据库中我有一个时间戳字段,值是:

2013-01-30 01:15:00
2013-01-30 01:20:00

我使用日期函数将其打印为 24HOUR:MINUTE 格式,但我得到了错误的时间结果。

mysql_query('SELECT time FROM locations ORDER BY time ASC');
...
echo "<td>" . date('H:i',$row['time']) . "</td>";

两条记录的输出都是:00:33,为什么?

最佳答案

date() 的第二个参数是 Unix 时间戳。

Description

string date ( string $format [, int $timestamp = time() ] )

Returns a string formatted according to the given format string using the given integer timestamp or the current time if no timestamp is given. In other words, timestamp is optional and defaults to the value of time().

echo "<td>" . date('H:i', strtotime($row['time'])) . "</td>";

另一种解决方案是使用 DateTime:

$datetime = new DateTime($row['time']);
echo $datetime->format('H:i');

// or in PHP5.5+
echo (new DateTime($row['time']))->format('H:i');

引用:

关于php 日期函数返回一个我没想到的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14594828/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com