如何更改模型中接收到的数据?
我在模型中有一个典型的函数,它从表“Orders”接收“时间”:
public function get_time() {
$result = array();
$result = DB::select('id', 'time', 'description')->from('orders')->execute();
return $result;
}
问题是,“时间”字段以格式(TIME)存储在 MySQL 中:'HH:mm:ss',例如:11:45:00。
但我不需要秒,所以我可以这样做:date('H:i', strtotime($time));
在视图中执行此操作不是一个好主意。我需要在模型或控制器中进行此转换。 当然:
$result['time'] = date('H:i', strtotime(result['time']));
行不通;)
I have a typical function in model which is receiving 'time' from table 'Orders':
public function get_time() {
$result = array();
$result = DB::select('id', 'time', 'description')->from('orders')->execute();
return $result;
}
The problem is, that 'time' field is stored in MySQL in format (TIME): 'HH:mm:ss'
, example: 11:45:00.
But I don't need seconds, so I can do: date('H:i', strtotime($time));
Doing this in a View isn't good idea. I need to do this conversion in Model or Controller.
Of course:
$result['time'] = date('H:i', strtotime(result['time']));
won't work ;)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在视图中执行此操作是完美的,它正在格式化模型结果以正确显示。
存在语法错误,您忘记在
result
前面加上美元符号。zombor说的也很重要(我错过了)
Doing it in the view is perfect, it's formatting the model result to be displayed correctly.
There is a syntax error, you forgot to prefix
result
with the dollar sign.What zombor said was important too (I missed it)
看一下 TIME_FORMAT 函数:
结果为
11:45
。像这样更改代码:Have a look at TIME_FORMAT function:
result in
11:45
. Change the code like this: