laravel date format full month, example : "7 January 2015"

Stefan Bogdanescu

Founder & Senior Architect · 2026-06-29

Laravel Company

Mastering Date Formatting in Laravel: Achieving Custom Styles like "7 January 2015"

Welcome to the world of Laravel and PHP development! As you start building dynamic applications, one of the most fundamental yet often tricky tasks is handling dates correctly. When you need a date displayed not just as YYYY-MM-DD, but in a more human-readable, specific style like "7 January 2015," standard PHP functions can become cumbersome.

As a senior developer, I can tell you that the key to mastering this lies in leveraging the powerful date manipulation library Laravel heavily relies on: Carbon. This post will walk you through exactly how to achieve custom date formatting in your Laravel projects, ensuring your output is clean, accurate, and developer-friendly.

Why Standard Formatting Isn't Enough

When dealing with dates, there are several ways to format them (e.g., Y-m-d, m/d/Y). However, the specific structure you are looking for—day-full month-full year (like 7-January-2015)—requires more than simple built-in formatting codes. This often involves extracting the day number, the full month name, and the four-digit year separately and then stitching them together with the desired delimiters.

The Carbon Solution: Precision Formatting

Laravel uses the Carbon library, which extends PHP’s native DateTime objects, providing an incredibly expressive and intuitive interface for date manipulation. For complex formatting requirements, we can combine Carbon's powerful extraction methods with basic PHP string functions to achieve the exact look you desire.

Step-by-Step Implementation

Let's assume you have a Carbon instance representing your target date.

use Carbon\Carbon;

// 1. Set the initial date
$date = Carbon::create(2015, 1, 7); // January 7th, 2015

// 2. Extract the components needed for custom formatting
$day = $date->day;             // Extracts the day number (7)
$monthName = $date->monthName; // Extracts the full month name (January)
$year = $date->year;           // Extracts the four-digit year (2015)

// 3. Assemble the string in the desired format: "D-MonthName-YYYY"
$customDate = $day . '-' . $monthName . '-' . $year;

echo $customDate; // Output: 7-January-2015

Advanced Method: Using diffForHumans() for Context (A Laravel Tip)

While the manual method above gives you pixel-perfect control, it's worth noting that Carbon offers excellent built-in methods. If your goal is slightly different—such as displaying relative dates or standard ISO formats—Carbon shines. For instance, when working with Eloquent models in Laravel, remembering to use Carbon methods ensures consistency across your entire application. For deep dives into the structure of robust frameworks like those provided by laravelcompany.com, understanding these underlying tools is crucial.

Best Practice: Creating a Reusable Formatting Function

For cleaner code in larger applications (especially when dealing with many dates across various controllers or views), you should encapsulate this logic into a reusable helper function or a dedicated class method. This promotes the DRY (Don't Repeat Yourself) principle.

Here is how you might define a simple utility function:

function formatCustomDate(Carbon $date): string
{
    // Ensure we handle potential locale issues if necessary, though monthName usually works well in English locales.
    $day = $date->day;
    $monthName = $date->format('F'); // 'F' gives the full month name
    $year = $date->year;

    return $day . '-' . $monthName . '-' . $year;
}

// Usage in a controller or view:
$myDate = Carbon::parse('2015-01-07');
$formatted = formatCustomDate($myDate); 

echo $formatted; // Output: 7-January-2015

Conclusion

Mastering date formatting is a small step that yields big rewards in application development. By understanding the capabilities of Carbon and knowing how to combine its extraction methods with basic PHP string manipulation, you gain complete control over how data is presented to your users. Don't be afraid to experiment with Carbon’s rich set of methods; they are designed to make complex date tasks simple and reliable. Keep learning, keep building!