Php 我怎么能只显示小时和分钟?
我在使用mysql的php中工作 这是我的函数,它以json格式返回结果Php 我怎么能只显示小时和分钟?,php,mysql,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_nu
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 TIME_FORMAT(`sun_open_time`, '%H %i') AS `opentime`;
这将直接从查询中返回所需的值,而无需任何PHP攻击。有很多方法可以做到这一点,最“干净”的方法是在SELECT子句中指定列,并用如下方式传递日期:
DATE\u格式(sun\u open\u time,“%H:%i”)
您的table place\u timing字段(sun_open_time、sun_close_time等..)只有hh:mm:ss格式的时间,所以必须使用MySql的SUBSTRING()函数
所以您可以如下查询
$sql=“选择子字符串(sun\u open\u time
,1,5),
子串(sun\u close\u time
,1,5),子串(mon\u open\u time
,1,5),
子串(mon\u close\u time
,1,5),
子字符串(tue\u open\u time
,1,5),子字符串(tue\u close\u time
,1,5)来自
place\u time
其中placeId
='$placeId'
这属于标准类别,例如,如果sun_open_time的数据只有时间格式(如hh:mm:ss),这将不起作用。它将同时适用于时间戳(最常用的类型)和日期时间;我不确定它是否适用于时间,但即使如此,您也可以切换到时间格式()。