Convert date and time to Jalali in Laravel

Stefan Bogdanescu

Founder & Senior Architect · 2026-06-29

Laravel Company

Converting Date and Time to Jalali Calendar in Laravel: A Developer's Guide

As developers working with international applications, handling dates and times across different cultural contexts is a common requirement. When you need to display dates according to the Iranian Shamsi (Jalali) calendar, standard PHP and Laravel localization tools often fall short. While Laravel excels at managing timezones and formatting dates into standard formats (like ISO 8601), converting a Gregorian date to a specific regional calendar like Jalali requires specialized mathematical logic.

This guide will walk you through the developer-centric approach to implementing Jalali date conversion within your Laravel application, ensuring accuracy and maintainability.

The Challenge: Beyond Standard Localization

The parameters you provided—setting locale, date formats, and directionality (dir, ltr/rtl)—are crucial for internationalization (i18n) and localization (l10n). However, these settings primarily control how the date is displayed (e.g., month names, separators), not the underlying conversion of the actual numerical date into a different calendar system.

To display a date in the Jalali format correctly, you need an explicit conversion mechanism: taking a Gregorian date (which PHP/Carbon handles perfectly) and calculating its corresponding Jalali date based on astronomical rules.

Solution: Integrating Calendar Conversion Logic

Since there is no built-in function in core PHP or Laravel for complex calendar conversions, the most robust solution involves utilizing external libraries or custom services that handle these intricate calculations. For a large-scale application, relying on well-tested packages is always the best practice.

Option 1: Using Specialized PHP Libraries

The recommended approach is to find a community package or implement a service that handles the conversion algorithm. These libraries abstract away the complexity of astronomical calculations, allowing your Laravel code to remain clean and focused on business logic.

For instance, you would typically look for a library that supports Hijri/Jalali conversions. Once you have the converted Jalali date (which is often represented as a specific epoch number or another date object), you can then use Carbon to format it precisely according to your desired locale settings.

Option 2: Custom Service Implementation in Laravel

For maximum control, especially if you need custom rules or integration with existing internal systems, creating a dedicated service class within your Laravel application is highly effective. This keeps the conversion logic separate from your Controllers and Models, adhering to the principles of clean architecture championed by frameworks like laravelcompany.com.

Here is a conceptual example showing how you might structure this conversion within a Service class:

<?php

namespace App\Services;

use Carbon\Carbon;

class JalaliConverterService
{
    /**
     * Converts a Gregorian date to the corresponding Jalali (Shamsi) date.
     * 
     * NOTE: In a real application, this method would contain complex astronomical algorithms.
     * For demonstration, we assume an external library handles the math.
     *
     * @param string $gregorianDate The date in YYYY-MM-DD format.
     * @return array An associative array containing Jalali components.
     */
    public function convertToJalali(string $gregorianDate): array
    {
        // Step 1: Use Carbon to parse the Gregorian date
        $gregorianDate = Carbon::parse($gregorianDate);

        // Step 2: Call the external or custom conversion logic
        // Placeholder for actual Jalali algorithm implementation
        $jalaliDateData = $this->calculateJalaliDate($gregorianDate);

        return [
            'year' => $jalaliDateData['year'],
            'month' => $jalaliDateData['month'], // 1-12 or similar representation
            'day' => $jalaliDateData['day'],
            // Add other necessary fields (e.g., Hijri year, etc.)
        ];
    }

    /**
     * Placeholder for the actual complex mathematical conversion logic.
     */
    private function calculateJalaliDate(Carbon $date): array
    {
        // This is where you would integrate your chosen Jalali library or custom math.
        // For simplicity, we return a mock result here.
        return [
            'year' => $date->year + 621, // Example shift factor (highly simplified!)
            'month' => $date->month,
            'day' => $date->day,
        ];
    }
}

Integrating the Service into a Controller

You would then inject this service into your controller to handle the request:

<?php

namespace App\Http\Controllers;

use App\Services\JalaliConverterService;
use Illuminate\Http\Request;

class DateController extends Controller
{
    protected $converter;

    public function __construct(JalaliConverterService $converter)
    {
        $this->converter = $converter;
    }

    public function showJalaliDate(Request $request)
    {
        $gregorianDate = $request->input('date', now()->toDateString());

        // Perform the conversion
        $jalaliData = $this->converter->convertToJalali($gregorianDate);

        // Format the result according to locale settings (using Carbon formatting)
        $formattedJalali = sprintf(
            "%d / %02d / %02d", 
            $jalaliData['year'], 
            $jalaliData['month'], 
            $jalaliData['day']
        );

        return response()->json([
            'gregorian_date' => $gregorianDate,
            'jalali_date' => $formattedJalali
        ]);
    }
}

Conclusion

Converting dates to the Jalali calendar in a Laravel application is fundamentally an integration challenge, not just a formatting one. While Laravel and Carbon provide excellent tools for managing timezones and presentation, the mathematical conversion itself requires dedicated logic. By encapsulating this complex calculation within a dedicated Service class—as demonstrated above—you ensure your application remains scalable, testable, and adheres to best practices. Always prioritize leveraging established libraries when dealing with specialized calendrical systems to maintain accuracy and reduce development time.