繁体   English   中英

如何从mysql PDO选择查询中的时间戳中仅选择时间?

[英]How to select only time from timestamp in mysql PDO select query?

因为它目前代码下面的代码只打印出完整的时间戳,如“2016-03-31 13:22:06”但我只想选择时间只有“13:22:06”甚至只是“13:22”。

只是想知道如何在PDO select语句中将它们拆分为仅占用时间。

感谢您的帮助。

/*** The SQL SELECT statement ***/

$sql = "SELECT * FROM chatLogTwo";
foreach ($dbh->query($sql) as $row)
{
 print $row['time'] .' - '. $row['message'] . '<br />';
}

你可以尝试使用date()strtotime() -

print date('H:i:s', strtotime($row['time'])) .' - '. $row['message'] . '<br />';

date()strtotime()

或者更改查询 -

"SELECT DATE_FORMAT(`time`, '%H:%i:%s') `time`, message FROM chatLogTwo"

日期格式()

你也可以试试这个:

Select DATE_FORMAT(timecol,"%h:%i") AS time FROM chatLogTwo

相反,您只需将查询更新为as

SELECT *,Date_format(`time`,'%H:%i') as only_time FROM chatLogTwo

并简单地在您的PHP代码中访问它,如as

echo $row['only_time'];

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM