Laravel - Getting Current Month and Year only
Stefan Bogdanescu
Founder & Senior Architect · 2026-06-29
# 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](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 --}}
{{-- Get the current month number (1-12) --}}
Month: {{ now()->month }}
{{-- Get the current year --}} Year: {{ now()->year }}
{{-- If you were using your example structure, it would look like this: --}}
{{-- Get the current year --}} Year: {{ now()->year }}
Current Month & Year: {{ now()->format('F Y') }}
``` 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 $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 --}}The current date displayed is: {{ $formattedDate }}
Month Number: {{ $currentMonth }}
Year: {{ $currentYear }}
``` ## 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!