Laravel Eloquent where between two dates from Database

Laravel Eloquent where between two dates from Database

In this post we will give you information about Laravel Eloquent where between two dates from Database. Hear we will give you detail about Laravel Eloquent where between two dates from DatabaseAnd how to use it also give you demo for it if it is necessary.

Laravel whereBetween method are useful when you retrieve records in between two given dates from timestamp column.

Suppose you want to filter records in database table by from data and to date then you can use whereBetween method to filter the records.

  1. if($request->input('from_date')<>''&&$request->input('to_date')<>'')
  2.     {    
  3.         $start=date("Y-m-d",strtotime($request->input('from_date')));
  4.         $end=date("Y-m-d",strtotime($request->input('to_date')."+1 day"));
  5.         $query->whereBetween('created_at',[$start,$end]);
  6.     }
if ($request->input('from_date')<>'' && $request->input('to_date')<>'')    {           $start = date("Y-m-d",strtotime($request->input('from_date')));        $end = date("Y-m-d",strtotime($request->input('to_date')."+1 day"));        $query->whereBetween('created_at',[$start,$end]);    }

Display date difference only in days using Laravel Carbon

  1. $created_at=newCarbon($product->created_at);
  2. $now= Carbon::now();
  3. $difference=($created_at->diff($now)->days <1)
  4. ?'today'
  5. :$created_at->diffForHumans($now);
$created_at = new Carbon($product->created_at);$now = Carbon::now();$difference = ($created_at->diff($now)->days < 1)    ? 'today'    : $created_at->diffForHumans($now);

Difference between two dates in a number of formats

  1. $to_date= Carbon::createFromDate('Y-m-d H:s:i','2013-5-5 5:10:12');
  2. $from_date= Carbon::createFromDate('Y-m-d H:s:i','2014-6-6 7:20:14');
  3. $diffInHours=$to_date->diffInHours($from_date, false);
  4. $diffInMinutes=$to_date->diffInMinutes($from_date, false);
  5. $diffInDays=$to_date->diffInDays($from_date, false);
$to_date = Carbon::createFromDate('Y-m-d H:s:i', '2013-5-5 5:10:12');$from_date = Carbon::createFromDate('Y-m-d H:s:i', '2014-6-6 7:20:14');$diffInHours = $to_date->diffInHours($from_date, false);$diffInMinutes = $to_date->diffInMinutes($from_date, false);$diffInDays = $to_date->diffInDays($from_date, false);
By using carbon its easier to work with dates in Laravel.

Hope this code and post will helped you for implement Laravel Eloquent where between two dates from Database. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

Leave a Comment

Your email address will not be published. Required fields are marked *

46  +    =  49

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US