简体   繁体   English

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

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

As it currently stands the code below only prints out the complete time stamp as in "2016-03-31 13:22:06" but i would only like to select the time only so "13:22:06" or even just the "13:22". 因为它目前代码下面的代码只打印出完整的时间戳,如“2016-03-31 13:22:06”但我只想选择时间只有“13:22:06”甚至只是“13:22”。

Just wanted to know how to split these up within a PDO select statement to only grab the time. 只是想知道如何在PDO select语句中将它们拆分为仅占用时间。

Thanks for your help in advance. 感谢您的帮助。

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

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

You can try with date() & strtotime() - 你可以尝试使用date()strtotime() -

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

date() & strtotime() date()strtotime()

Or change the query - 或者更改查询 -

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

DATE_FORMAT() 日期格式()

你也可以试试这个:

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

Instead you can simply update your query into something like as 相反,您只需将查询更新为as

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

and simply access it at your PHP code like as 并简单地在您的PHP代码中访问它,如as

echo $row['only_time'];

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

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