dongzhukuai8177 2014-07-04 20:44
浏览 63
已采纳

在PHP Select语句中将MYSQL时间从HH:MM:SS转换为HH:MM

I am using a PHP Select statement to pull data from MySQL. The eventTime field is stored as TIME in the schema as HH:MM:SS but I only want HH:MM. I have tried using TIME_FORMAT(eventTime, '%H:%i') in the select statement but it does not work. Any ideas? I have even tried to convert the variable in PHP but I am stumped. Please help me identify what I'm missing.

$query="SELECT id,eventDate,eventTime, eventName FROM specialevents ORDER BY eventDate,eventTime ASC";

$result=$mysqliConnection->query($query,MYSQLI_STORE_RESULT);

while($row =$result->fetch_object())
  {


     $id=$row->id;
     $eventDate=$row->eventDate;
     $eventTime=$row->eventTime;
     $eventName=$row->eventName;
     $reportTime=$row->reportTime;

 echo '<tr>’; 
 echo '<td class="tdId"><input type="text" class="tbId" value="' . $id . '"/></td>’; 
 echo '<td class="tdEventDate"><input type="text" class="tbEventDate" value="' . $eventDate . '"/></td>’; 
 echo '<td class="tdEventTime"><input type="text" class="tbEventTime" value="' . $eventTime . '"/></td>’; 
 echo '<td class="tdEventName"><input type="text" class="tbEventName" value=" ' . $eventTime . '"/></td>’; 
 echo '<td class="tdReportTime"><input type="text" class="tbReportTime" value=" ' . $reportTime . '"/></td>';   

}
  • 写回答

3条回答 默认 最新

  • doumang20060820 2014-07-05 03:53
    关注

    You shouldn't be storing date and time separately if they refer to the same entity. However, you can use MySQL's TIME_FORMAT() function to format your time:

    TIME_FORMAT(date,format)
    

    Formats the date value according to the format string.

    $query="SELECT id, eventDate, TIME_FORMAT(eventTime, '%H:%i') AS eventTime, eventName 
    FROM specialevents 
    ORDER BY eventDate, eventTime ASC";
    

    You can use the specifiers from DATE_FORMAT to format as you wish.

    Also, the ASC sort order is default and can be omitted from your query.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog