php - 我怎样才能只显示小时和分钟?

标签 php mysql

我正在使用 mysql 的 php 中工作。

这是我的函数,它以 json 格式返回结果。

function getTiming($placeId) {

  $sql = "SELECT * from place_timing where placeId='$placeId'";

    $result = mysql_query($sql) or die ("Query error: " . mysql_error());

    $records = array();

    if (mysql_num_rows($result)==0)

     {

      echo '['.json_encode(array('placeId' => 0)).']';
      //this is for null result

     }

     else

     {

       while($row = mysql_fetch_assoc($result)) 

       {

         $records[] = $row;

       }


       echo json_encode($records); //this concerts in json

     }

}

这个函数的输出是这样的:-

[{"placeId":"31","sun_open_time":"00:00:00","sun_close_time":"00:00:00","mon_open_time":"00:00:00","mon_close_time":"23:00:00","tue_open_time":"00:00:00","tue_close_time":"00:00:00"}]

但我只想显示小时:分钟。意味着我不想显示秒。

请建议我如何更改上述功能,以便我只能显示小时:分钟。

提前谢谢您。

最佳答案

有很多方法可以做到这一点,“最干净的”是在 SELECT 子句中指定列并使用 DATE_FORMAT() 传递日期。像这样:DATE_FORMAT(sun_open_time, '%H:%i')

关于php - 我怎样才能只显示小时和分钟?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8573278/

相关文章:

MySql更新查询

php - MySQL 在备份运行时复制插入查询

php - 在javascript中将json文件加载到数组中

php数据库连接错误

mysql - mysql中SUM的问题

使用游标和临时表时的 MySQL 存储过程性能问题

php - 选择、编辑数据,然后将数据插入新数据库

PHP 读取 UTF-8 CSV

php - 什么不能忘记?

mysql - 查询 : Select and display next highest value as new column