PHP code example of danialzash / laravel-jalali

1. Go to this page and download the library: Download danialzash/laravel-jalali library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

danialzash / laravel-jalali example snippets


return [
    'default_format' => 'datetime', // Options: 'datetime', 'date', 'time', or custom format
];

// Current date/time in Jalali
jalali();

// Convert a specific date
jalali('2024-03-20');

// With custom format
jalali(now(), 'Y/m/d');

// From a Carbon instance
jalali($user->created_at, 'l j F Y');

use Danialzash\LaravelJalali\Facades\Jalali;

// Get a Jalalian instance
$jalaliDate = Jalali::from(now());

// Format directly
$formatted = Jalali::format(now(), 'Y-m-d H:i:s');

use Illuminate\Support\Carbon;

// Get a Jalalian instance from Carbon
$jalali = Carbon::now()->toJalali();

// Get a formatted Jalali string directly
$dateString = Carbon::now()->toJalaliString('Y/m/d');

// Works with any Carbon instance
$user->created_at->toJalaliString(); // e.g., "1402/12/30 14:30:00"

$jalali = now()->toJalali();

$jalali->getYear();       // 1402
$jalali->getMonth();      // 12
$jalali->getDay();        // 30
$jalali->format('l j F'); // پنجشنبه 30 اسفند
$jalali->ago();           // 2 ساعت پیش

// In a Blade template
<p>تاریخ ثبت‌نام: {{ $user->created_at->toJalaliString('Y/m/d') }}</p>

// In a controller
public function show(User $user)
{
    return [
        'name' => $user->name,
        'joined' => jalali($user->created_at, 'j F Y'),
    ];
}

// With Eloquent
$posts = Post::latest()->get()->map(fn ($post) => [
    'title' => $post->title,
    'date' => $post->created_at->toJalaliString('Y/m/d'),
]);
bash
php artisan vendor:publish --tag=jalali-config