Laravel - Getting Current Month and Year only

Stefan Bogdanescu

Founder & Senior Architect · 2026-06-29

Laravel Company

Laravel: Getting the Current Month and Year Dynamically for Your Views

Welcome to the world of Laravel! It’s exciting that you are diving into building applications with this powerful framework. As you start learning, one of the first things you'll realize is how much magic Laravel allows you to perform with simple code. Today, we're going to solve a very common requirement: dynamically displaying the current month and year on a label or form field in your Blade templates.

As a senior developer, I can tell you that the key to handling dates and times in any modern PHP framework—and especially Laravel—lies in using the powerful Carbon library. Carbon extends the standard PHP DateTime class, making date manipulation intuitive, readable, and extremely robust.

Why Use Carbon for Date Handling?

When you are working with dates in Laravel, relying on raw PHP functions can quickly become messy. By leveraging Carbon, you get methods like now(), month, year, and easy formatting tools. This keeps your code clean, readable, and highly maintainable. For any serious project, understanding how to leverage these date utilities is fundamental to mastering the Laravel ecosystem, much like understanding Eloquent relationships is crucial when using a framework like https://laravelcompany.com.

Method 1: Getting the Date Directly in the Blade View (Simple Display)

If you only need to display the current date right when the page loads and don't need to save it to the database, you can calculate it directly within your Blade file using Carbon.

Here is how you can inject the current month and year into your label:

html
{{-- Assuming you are using a standard Blade setup --}}

<div>
    {{-- Get the current month number (1-12) --}}
    Month: {{ now()->month }}

    <br>

    {{-- Get the current year --}}
    Year: {{ now()->year }}
</div>

{{-- If you were using your example structure, it would look like this: --}}
<label for="title">Title (Current Date)</label>
<p>Current Month & Year: {{ now()->format('F Y') }}</p>

In the example above, we use now() to get the current timestamp and then access the specific properties: ->month and ->year. We also demonstrated using the ->format('F Y') method, which is extremely useful for creating nicely formatted strings (e.g., "October 2023").

Method 2: Preparing Data in the Controller (Best Practice)

While Method 1 is fast for simple display, the best practice in Laravel development is to keep your Controllers focused on business logic and let the Blade templates handle presentation. If you need this date information to be part of a larger data structure or if you want to reuse it across multiple parts of your view, calculate it in your Controller first.

Controller Example:

php
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Carbon\Carbon; // Make sure to import Carbon

class DateController extends Controller
{
    public function showDate()
    {
        // Create a Carbon instance for the current time
        $now = Carbon::now();

        return view('date_display', [
            'currentMonth' => $now->month,
            'currentYear' => $now->year,
            'formattedDate' => $now->format('F Y')
        ]);
    }
}

Blade View Example (date_display.blade.php):

Now, in your view, you simply consume the data passed from the controller:

html
{{-- Assuming data is passed via the view array --}}
<label for="title">Title</label>
<p>The current date displayed is: {{ $formattedDate }}</p>
<p>Month Number: {{ $currentMonth }}</p>
<p>Year: {{ $currentYear }}</p>

Conclusion

Whether you are displaying a simple, immediate piece of information or preparing complex data for your application, mastering date handling with Carbon is non-negotiable in Laravel. Start by understanding how to use the Carbon class—it will save you countless hours of debugging date-related issues. As you continue building your projects, remember that robust architecture starts with solid tools. Keep exploring the documentation and embrace the power of the Laravel ecosystem!