首页 > 解决方案 > 在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
        )
)

我想在选择时获得当前时间。有没有办法得到它?谢谢

标签: laraveleloquentlaravel-8

解决方案


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


推荐阅读