R Fauzdar
R Fauzdar

Reputation: 241

How can i show only hour and minutes?

I am working in php in which i am using mysql.

This is my function which is returning result in json format.

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

     }

}

output of this function is like this:-

[{"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"}]

But I want to show only hour:minutes. means I don't want to show seconds.

please suggest me how can I change my above function so I can show only hour:minutes.

Thank you in advance.

Upvotes: 2

Views: 9991

Answers (3)

Kammy
Kammy

Reputation: 431

It seems your fields of table place_timing (sun_open_time, sun_close_time and so on..) have only hh:mm:ss formated time. so you have to use SUBSTRING() function of MySql.

So you query could be as follows.

$sql = "SELECT SUBSTRING(sun_open_time,1,5), SUBSTRING(sun_close_time,1,5), SUBSTRING(mon_open_time,1,5), SUBSTRING(mon_close_time,1,5), SUBSTRING(tue_open_time,1,5),SUBSTRING(tue_close_time,1,5) from place_timing where placeId='$placeId'";

Upvotes: 0

Viruzzo
Viruzzo

Reputation: 3025

There are many ways to do it, the "cleanest" would be to specify the columns in the SELECT clause and pass the dates with DATE_FORMAT() like this: DATE_FORMAT(sun_open_time, '%H:%i').

Upvotes: 4

Oldskool
Oldskool

Reputation: 34837

Adjust your query to select the actual fields, rather then * and then use TIME_FORMAT() on the time fields, like:

SELECT TIME_FORMAT(`sun_open_time`, '%H %i') AS `opentime`;

That will return the desired values directly from the query without any PHP hacks.

Upvotes: 1

Related Questions