我在mysql数据库中有一列时间类型,我想将其转换为php时间对象以便能够在某些函数中使用它,我尝试使用strtotime函数,但这对我不起作用,这是我的PHP代码:
$result=mysql_query("select time,name from users where user_id='1'");
if($result)
{
$row=mysql_fetch_array($result);
$time=$row['time'];
$formatted_time=strtotime($time);
echo date('H:i:s',$formatted_time);
}
但我总是得到的是: 01:00:00
我不知道这1个小时是从哪里来的!
我知道这个问题可能是重复的,但所有的答案都在谈论使用 strtotime 函数,但正如你所看到的那样不起作用。你能帮忙恳求吗?
提前感谢
使用TIME_FORMAT:
SELECT TIME_FORMAT('14:30:00', '%h:%i %p'); /* 02:30 PM */
TIME_FORMAT是一个mysql
函数,你可以在查询中使用它!
有关"TIME_FORMAT"的详细信息,请单击此处
$result=mysql_query("select TIME_FORMAT(`time`, '%h:%i %p') as time, `name` from users where user_id='1'");
if ($result)
{
$row=mysql_fetch_array($result);
$time=$row['time'];
echo $time;
}