laravel - 在laravel中获取当前时间
问题描述
$product = Product::select('id', 'name', 'status',DB::raw('date(\'d-m-y\') as dateCurrent' ))
->whereDate('status', 3)
->get()
->toArray();
结果dateCurrent
空:
Array
(
[0] => Array
(
[id] => 4
[name] => 'Iphone'
[dateCurrent] => null
)
[1] => Array
(
[id] => 5
[name] => 'samsung'
[dateCurrent] => null
)
)
我想在选择时获得当前时间。有没有办法得到它?谢谢
解决方案
date('d-m-y')
看起来像 php 构建的函数,如果你想分贝时间,你可以使用 NOW() 函数,如果你使用分贝时间
$product = Product::select('id', 'name', 'status', DB::raw('NOW() AS dateCurrent'))
->whereDate('status', 3)
->get()
->toArray();
但你想要php服务器时间
$product = Product::select(
'id', 'name',
'status', DB::raw(sprintf('%s AS dateCurrent', date('d-m-y'))))
->whereDate('status', 3)
->get()
->toArray();
https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_now
推荐阅读
- java - 使我们的前端 Typescript 类型与我们的后端源代码类型保持一致
- r - 按行提取两个字符串之间的数据
- javascript - 如何将增量应用于 ace.js 中的字符串
- excel - Range.Formula 中的应用程序定义或对象定义错误 1004
- system-verilog - 有没有办法在 systemverilog 中“映射”数组?
- amazon-web-services - iam:PassRole api 执行什么操作?
- python - 如何从 Python 中的特定文件中调用随机图片?
- python-3.x - 如何在不需要拟合数据的情况下定义高斯混合模型?
- c++ - 在使用 CreateObject 从 VBScript 中通过 COM 引用 C++ DLL 时,如何处理 DLL 版本?
- c - break 语句在 else 语句中不起作用