How To Count Days Between Two Dates in Laravel - TechvBlogs

How To Count Days Between Two Dates in Laravel

In this short article, you will learn How to count days between two dates in Laravel.


Suresh Ramani - Author - TechvBlogs
Suresh Ramani
 

1 week ago

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 App\Http\Controllers;
  
use Illuminate\Http\Request;
use Carbon\Carbon;
  
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 App\Http\Controllers;
  
use Illuminate\Http\Request;
use Carbon\Carbon;
  
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 App\Http\Controllers;
  
use Illuminate\Http\Request;
use Carbon\Carbon;
  
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 App\Http\Controllers;
  
use Illuminate\Http\Request;
use Carbon\Carbon;
  
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.

Read Also: How to Update Node.js to the Latest Version on Ubuntu

If you want to manage your VPS / VM Server without touching the command line go and  Checkout this linkServerAvatar allows you to quickly set up WordPress or Custom PHP websites on VPS / VM in a  matter of minutes.  You can host multiple websites on a single VPS / VM, configure SSL certificates, and monitor the health of your server without ever touching the command line interface.

Comments (0)

ServerAvatar
Multiple sites on a single VPS/VM/Server Deploy PHP sites on cloud in minutes, simple as that!
ServerAvatar
Generate Free SSL Certificate - SSLForWeb

Generate Free SSL Certificate

  • 90-Day Certificates
  • SSL Monitoring
  • Wildcard Certificates
  • Multiple Domains
  • Certificate Management
  • Expiration Reminders

Comment


Note: All Input Fields are required.