我在模型中有一个典型的函数,它从表'Orders'接收'time':
public function get_time() {
$result = array();
$result = DB::select('id', 'time', 'description')->from('orders')->execute();
return $result;
}
问题是,'time'字段以格式(TIME)存储在MySQL中:'HH:mm:ss'
,例如:11:45:00。
但我不需要秒,所以我可以这样做:date('H:i', strtotime($time));
在View中执行此操作并不是一个好主意。我需要在模型或控制器中进行此转换。
当然:
$result['time'] = date('H:i', strtotime(result['time']));
不起作用;)
答案 0 :(得分:2)
在视图中执行它是完美的,它将格式化模型结果以便正确显示。
$result['time'] = date('H:i', strtotime(result['time']));
语法错误,您忘记在result
前加上美元符号。
zombor说的很重要(我错过了)
$ result是数据库结果迭代器对象,而不是单个结果
答案 1 :(得分:0)
查看TIME_FORMAT函数:
SELECT TIME_FORMAT('11:45:00', '%H:%i')
结果为11:45
。像这样更改代码:
$result = DB::select('id', array('TIME_FORMAT("time", '%H:%i')', 'formatted_time'), 'description')->from('orders')->execute();