【问题标题】:How to get birthdate in Laravel?如何在 Laravel 中获得生日?
【发布时间】:2020-02-11 13:03:31
【问题描述】:

我想显示 first_name 和birth_date 的值 今天生日的顾客。我应该做什么样的查询?

  $birthday = DB::table('customers')
  ->select(DB::raw('customers.first_name','customers.birth_date'))
  ->whereRaw('DAYOFYEAR(curdate()) <= DAYOFYEAR(birth_date) AND DAYOFYEAR(curdate()) + 0 >=  dayofyear(birth_date)')
  ->orderByRaw('DAYOFYEAR(birth_date)')
  ->get();

【问题讨论】:

    标签: mysql laravel date


    【解决方案1】:

    你可以用这个:

     $birthday = DB::table('customers')
      ->select(DB::raw('customers.first_name','customers.birth_date'))
      ->whereRaw("DATE_FORMAT(FROM_UNIXTIME(birth_date),'%m-%d') = DATE_FORMAT(NOW(),'%m-%d')")
      ->orderByRaw('DAYOFYEAR(birth_date)')
      ->get();
    

    参考: Use MySQL to determine whether today is a user's birthday

    【讨论】:

    • databsae可以查询,php不能查询。
    • 为什么要使用php来获取今天的生日?
    • 我想在我的管理控制台中显示我的客户的生日值,让员工知道。
    • 此查询将返回所有今天有 b'dy 的客户。您可以使用 $birthday->birth_date 轻松向这些客户展示
    【解决方案2】:

    您可以使用以下查询获得结果...

    $today = \Carbon\Carbon::today()->toDateString();
    
    $birthday = DB::table('customers')
        ->select('customers.first_name','customers.birth_date')
        ->whereDate('birth_date', $today)
        ->get();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多