Reputation: 241
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
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) fromplace_timing
whereplaceId
='$placeId'";
Upvotes: 0
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
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