【问题标题】:How to query data from a table where **created_at** date = **today's date** ?如何从 **created_at** date = **today's date** 的表中查询数据?
【发布时间】:2015-03-02 04:00:42
【问题描述】:

我想从 inventories 表中查询数据,其中 created_at date = today date

我如何在 Laravel 中做到这一点?我试过了

date_default_timezone_set ('America/New_York');
$today = date("d"); // 18 ( since today is 2/18/2015 )

$inventories = Inventory::where( 'created_at' ,'=', $today )->get();

我得到 0 个结果。

【问题讨论】:

    标签: php mysql laravel laravel-4


    【解决方案1】:

    您不能只将月份中的某天与时间戳进行比较。试试这个:

    $today = Carbon::now()->toDateString();
    $inventories = Inventory::whereRaw('DATE(created_at) = ?', [$today])->get();
    

    背景信息:Carbon 是 Laravel 使用的 DateTime 库。它使事情变得更容易一些,但当然您也可以使用date() 获取当前日期。但是date("d") 不会返回当前日期,而实际上只是返回月份中的某一天。完整日期为date('Y-m-d')

    【讨论】:

    • 您知道如何将我的 created_at 日期转换为 day_of_week 吗?例如,因为今天 = 2/18/15 星期三将返回 3。我尝试使用 jddayofweek() ,但到目前为止没有运气。
    猜你喜欢
    • 2014-11-15
    • 2019-08-30
    • 1970-01-01
    • 1970-01-01
    • 2018-04-26
    • 2020-11-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多