如何更改模型中接收到的数据?

发布于 2024-12-08 10:21:33 字数 518 浏览 3 评论 0原文

我在模型中有一个典型的函数,它从表“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

月野兔 2024-12-15 10:21:33

在视图中执行此操作是完美的,它正在格式化模型结果以正确显示。

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

存在语法错误,您忘记在 result 前面加上美元符号。


zombor说的也很重要(我错过了)

$result 是数据库结果迭代器对象,而不是单个结果

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

自找没趣 2024-12-15 10:21:33

看一下 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();

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();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文