PHP code example of picobaz / jalaliflow

1. Go to this page and download the library: Download picobaz/jalaliflow 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/ */

    

picobaz / jalaliflow example snippets


   'aliases' => [
       'JalaliFlow' => PicoBaz\JalaliFlow\Facades\JalaliFlow::class,
   ],
   

return [
    'date_format' => 'Y/m/d', // Default Jalali date format
    'locale' => 'fa',        // Default language for date display
    'timezone' => 'Asia/Tehran', // Default timezone
    'holidays' => [
        '1404/01/01' => 'Norouz',
        '1404/01/02' => 'Norouz',
        // Add more holidays here
    ],
];

use PicoBaz\JalaliFlow\Facades\JalaliFlow;

// Convert Gregorian to Jalali
echo JalaliFlow::toJalali('2025-05-14'); // Output: 1404/02/24

// Convert Jalali to Gregorian
echo JalaliFlow::toGregorian('1404/02/24'); // Output: 2025-05-14

// Custom format
echo JalaliFlow::toJalali('2025-05-14', 'Y-m-d'); // Output: 1404-02-24

echo JalaliFlow::addDay('1404/02/24', 5);   // Output: 1404/02/29
echo JalaliFlow::addWeek('1404/02/24', 2);  // Output: 1404/03/08
echo JalaliFlow::addMonth('1404/02/24', 1); // Output: 1404/03/24
echo JalaliFlow::addYear('1404/02/24', 1);  // Output: 1405/02/24

echo JalaliFlow::subDay('1404/02/24', 5);   // Output: 1404/02/19
echo JalaliFlow::subWeek('1404/02/24', 2);  // Output: 1404/02/10
echo JalaliFlow::subMonth('1404/02/24', 1); // Output: 1404/01/24
echo JalaliFlow::subYear('1404/02/24', 1);  // Output: 1403/02/24

echo JalaliFlow::diff('1404/02/24', '1405/01/01', 'day');   // Output: ~312
echo JalaliFlow::diff('1404/02/24', '1405/01/01', 'week');  // Output: ~44.57
echo JalaliFlow::diff('1404/02/24', '1405/01/01', 'month'); // Output: ~10.3
echo JalaliFlow::diff('1404/02/24', '1405/01/01', 'year');  // Output: ~0.86

echo JalaliFlow::validateJalaliDate('1404/02/24') ? 'Valid' : 'Invalid'; // Output: Valid
echo JalaliFlow::validateJalaliDate('1404/12/31') ? 'Valid' : 'Invalid'; // Output: Invalid

use PicoBaz\JalaliFlow\Facades\JalaliFlow;

// Assuming today is 1404/02/24 (2025-05-22)
echo JalaliFlow::toRelativeJalali('2025-05-22'); // Output: امروز
echo JalaliFlow::toRelativeJalali('2025-05-21'); // Output: دیروز
echo JalaliFlow::toRelativeJalali('2025-05-24'); // Output: ۲ روز بعد
echo JalaliFlow::toRelativeJalali('2025-05-19'); // Output: ۳ روز پیش
echo JalaliFlow::toRelativeJalali('2025-05-19', 'Asia/Tehran', 'en'); // Output: 3 days ago

  use PicoBaz\JalaliFlow\Facades\JalaliFlow;

  $holidays = JalaliFlow::getHolidays(1404);
  foreach ($holidays as $date => $description) {
      echo "$date: $description\n";
  }
  // Example output:
  // 1404/01/01: نوروز
  // 1404/01/02: نوروز
  // 1404/01/03: نوروز
  // 1404/01/04: نوروز
  // 1404/01/13: روز طبیعت
  // 1404/12/29: روز ملی شدن صنعت نفت
  

  use PicoBaz\JalaliFlow\Facades\JalaliFlow;

  try {
      $date = '1404/01/01';
      echo JalaliFlow::isHoliday($date) ? "$date is a holiday" : "$date is not a holiday";
      // Output: 1404/01/01 is a holiday

      $date = '1404/02/24';
      echo JalaliFlow::isHoliday($date) ? "$date is a holiday" : "$date is not a holiday";
      // Output: 1404/02/24 is not a holiday
  } catch (Exception $e) {
      echo "Error: " . $e->getMessage();
  }
  

  use PicoBaz\JalaliFlow\Facades\JalaliFlow;

  try {
      JalaliFlow::addCustomHoliday('1404/06/15', 'Company Anniversary');
      echo JalaliFlow::isHoliday('1404/06/15') ? 'Custom holiday added' : 'Failed to add holiday';
      // Output: Custom holiday added

      // Verify in holidays list
      $holidays = JalaliFlow::getHolidays(1404);
      echo isset($holidays['1404/06/15']) ? "Found: {$holidays['1404/06/15']}" : 'Not found';
      // Output: Found: Company Anniversary
  } catch (Exception $e) {
      echo "Error: " . $e->getMessage();
  }
  

  use PicoBaz\JalaliFlow\Facades\JalaliFlow;

  try {
      $date = '1404/02/24';
      echo JalaliFlow::isWorkingDay($date) ? "$date is a working day" : "$date is not a working day";
      // Output: 1404/02/24 is a working day (assuming it's not a Friday)

      $date = '1404/01/01';
      echo JalaliFlow::isWorkingDay($date) ? "$date is a working day" : "$date is not a working day";
      // Output: 1404/01/01 is not a working day (Nowruz holiday)
  } catch (Exception $e) {
      echo "Error: " . $e->getMessage();
  }
  

  use PicoBaz\JalaliFlow\Facades\JalaliFlow;
  use Illuminate\Support\Facades\DB;

  // Example: Add a daily record to the database
  JalaliFlow::createEvent(
      name: 'Add Daily Record',
      frequency: 'daily',
      startDate: '1404/02/24',
      action: function () {
          DB::table('records')->insert(['created_at' => now()]);
      }
  );

  // Example: Call a class method monthly
  JalaliFlow::createEvent(
      name: 'Monthly Report',
      frequency: 'monthly',
      startDate: '1404/02/24',
      action: 'App\Services\ReportService@generateMonthly'
  );
  

  echo JalaliFlow::getNextRunDate('1404/02/24', 'daily');   // Output: 1404/02/25
  echo JalaliFlow::getNextRunDate('1404/02/24', 'weekly');  // Output: 1404/03/01
  echo JalaliFlow::getNextRunDate('1404/02/24', 'monthly'); // Output: 1404/03/24
  echo JalaliFlow::getNextRunDate('1404/02/24', 'yearly');  // Output: 1405/02/24
  

  use Illuminate\Support\Facades\Schedule;

  Schedule::command('jalali:run-events')->daily();
  

  protected function schedule(Schedule $schedule)
  {
      $schedule->command('jalali:run-events')->daily();
  }
  

use Illuminate\Database\Eloquent\Model;
use PicoBaz\JalaliFlow\Traits\JalaliDate;

class Post extends Model
{
    use JalaliDate;
}

$post = Post::first();
echo $post->jalali_date; // Output: 1404/02/24

  use PicoBaz\JalaliFlow\Facades\JalaliFlow;

  try {
      echo JalaliFlow::toHijri('2025-05-22', 'gregorian', 'Y/m/d');
      // Output: 1446/11/14
      echo JalaliFlow::toHijri('1404/02/24', 'jalali', 'Y/m/d');
      // Output: 1446/11/14
  } catch (Exception $e) {
      echo "Error: " . $e->getMessage();
  }
  

  use PicoBaz\JalaliFlow\Facades\JalaliFlow;

  try {
      echo JalaliFlow::fromHijri('1446/11/14', 'gregorian', 'Y-m-d');
      // Output: 2025-05-22
      echo JalaliFlow::fromHijri('1446/11/14', 'jalali', 'Y/m/d');
      // Output: 1404/02/24
  } catch (Exception $e) {
      echo "Error: " . $e->getMessage();
  }
  

  use PicoBaz\JalaliFlow\Facades\JalaliFlow;

  try {
      $date = '1404/04/25'; // Approximate date for Ashura 1446
      echo JalaliFlow::isIslamicHoliday($date, 'jalali') ? "$date is an Islamic holiday" : "$date is not an Islamic holiday";
      // Output: 1404/04/25 is an Islamic holiday (if Ashura)

      $date = '1404/02/24';
      echo JalaliFlow::isIslamicHoliday($date, 'jalali') ? "$date is an Islamic holiday" : "$date is not an Islamic holiday";
      // Output: 1404/02/24 is not an Islamic holiday
  } catch (Exception $e) {
      echo "Error: " . $e->getMessage();
  }
  

  use PicoBaz\JalaliFlow\Facades\JalaliFlow;

  try {
      $days = JalaliFlow::workingDaysBetween('1404/02/20', '1404/02/27');
      echo "Working days: $days";
      // Output: Working days: 5 (assuming no holidays, excluding Friday)

      $days = JalaliFlow::workingDaysBetween('1404/01/01', '1404/01/07');
      echo "Working days: $days";
      // Output: Working days: 2 (excluding Nowruz holidays and Friday)

      $days = JalaliFlow::workingDaysBetween('1404/02/24', '1404/02/24');
      echo "Working days: $days";
      // Output: Working days: 0 (same start and end date)
  } catch (Exception $e) {
      echo "Error: " . $e->getMessage();
  }
  

echo JalaliFlow::toJalali('2025-05-14', 'Y-m-d H:i:s'); // Output: 1404-02-24 00:00:00
bash
   php artisan vendor:publish --tag=jalaliflow
   
bash
   php artisan vendor:publish --tag=jalaliflow
   php artisan migrate
   
bash
  php artisan jalali:run-events
  
bash
  php artisan schedule:work
  
bash
php artisan jalali:holidays 1404

Holidays for 1404:
1404/01/01: Norouz
1404/01/02: Norouz