开发者

How to change received data in Model?

开发者 https://www.devze.com 2023-04-10 11:45 出处:网络
I have a typical function in model which is receiving \'time\' from table \'Orders\': public function get_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('开发者_如何学GoH:i', strtotime(result['time'])); 

won't work ;)


Doing it in the view is perfect, it's formatting the model result to be displayed correctly.

$result['time'] = date('H:i', strtotime(result['time'])); 

There is a syntax error, you forgot to prefix result with the dollar sign.


What zombor said was important too (I missed it)

$result is a database result iterator object, not a single result


Have a look at TIME_FORMAT function:

SELECT TIME_FORMAT('11:45:00', '%H:%i')   

result in 11:45. Change the code like this:

$result = DB::select('id', array('TIME_FORMAT("time", '%H:%i')', 'formatted_time'), 'description')->from('orders')->execute();
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号