【问题标题】:Laravel date format in where conditionLaravel 日期格式在 where 条件下
【发布时间】:2017-01-30 20:20:48
【问题描述】:

我有这个代码

use Carbon;
$now = Carbon::now()->toDateString(); 

所以我得到了没有时间的日期

public funtion test($brandid){

     $nbofsale=DB::table('st_nsales')
                         ->where('brand_id', $brandid)
                         ->where('eodate', $now)
                         ->get();
     return $nbofsale; 
}

选择中的eodate 字段是日期时间。我只想将它作为日期放在此查询中,而不是更改数据库中的字段类型。 有什么想法吗?

【问题讨论】:

    标签: php laravel php-carbon


    【解决方案1】:

    如果eodate是时间戳,则不需要转换为日期字符串:

    $nbofsale = DB::table('st_nsales')
                  ->where('brand_id', $brandid)
                  ->whereDate('eodata', Carbon::today()->toDateString());
                  ->get();
    

    获取今天记录的另一种方式:

    $nbofsale = DB::table('st_nsales')
                  ->where('brand_id', $brandid)
                  ->where('eodata', '>', Carbon::now()->startOfDay());
                  ->get();
    

    【讨论】:

    • 但是 Carbon::now();返回日期和时间,这就是我进行对话的原因。但我的问题是我想将eodate字段转换为没有时间的日期,所以查询条件比较了没有时间的2个日期
    • 在 mysql 中,eodate 类型是 DATETIME
    • @ElioChamy 尝试使用和不使用 toDateString() 的更新代码
    • whereDate() 缺少参数 3
    • @ElioChamy 第三个参数是可选的public function whereDate($column, $operator, $value = null, $boolean = 'and') 请再次检查您的代码是否有错误。我还添加了另一种获取今天数据的方法。
    猜你喜欢
    • 2019-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-11
    • 2021-02-22
    • 1970-01-01
    • 2012-02-25
    • 1970-01-01
    相关资源
    最近更新 更多