How To Count Days Between Two Dates in Laravel
In this post we will give you information about How To Count Days Between Two Dates in Laravel. Hear we will give you detail about How To Count Days Between Two Dates in Laravel And how to use it also give you demo for it if it is necessary.
There are so many different methods to count days between two dates in Laravel. In this article, We will use carbon for that. Also, Laravel uses carbon for Datetime.
What is Carbon in Laravel?
Carbon is a simple API extension of DateTime. Carbon makes it easy to get DateTime. Also, There are multiple methods to get data like specific timezone date, today date, yesterday, and many other methods. You can check it out.
How To Count Days Between Two Dates in Laravel
Here are some examples of count days between two dates in Laravel.
1. Count Days Between Two Dates in Laravel
<?php
namespace AppHttpControllers;
use IlluminateHttpRequest;
use CarbonCarbon;
class DateController extends Controller
{
public function index(Request $request)
{
$toDate = Carbon::parse("2022-11-20");
$fromDate = Carbon::parse("2022-11-24");
return $toDate->diffInDays($fromDate);
}
}
2. Count Months Between Two Dates in Laravel
<?php
namespace AppHttpControllers;
use IlluminateHttpRequest;
use CarbonCarbon;
class DateController extends Controller
{
public function index(Request $request)
{
$toDate = Carbon::parse("2022-01-01");
$fromDate = Carbon::parse("2022-11-01");
return $toDate->diffInMonths($fromDate);
}
}
3. Count Years Between Two Dates in Laravel
<?php
namespace AppHttpControllers;
use IlluminateHttpRequest;
use CarbonCarbon;
class DateController extends Controller
{
public function index(Request $request)
{
$toDate = Carbon::parse("2021-11-20");
$fromDate = Carbon::parse("2022-11-24");
return $toDate->diffInYears($fromDate);
}
}
4. Count the Days Between today’s Date and to Given Date in Laravel
<?php
namespace AppHttpControllers;
use IlluminateHttpRequest;
use CarbonCarbon;
class DateController extends Controller
{
public function index(Request $request)
{
$buyDate = Carbon::parse("2021-11-20");
return today()->diffInDays($buyDate);
}
}
Thank you for reading this blog.
Also see: How to Update Node.js to the Latest Version on Ubuntu
. .
Hope this code and post will helped you for implement How To Count Days Between Two Dates in Laravel. 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