129 lines
3.4 KiB
PHP
129 lines
3.4 KiB
PHP
<?php
|
|
|
|
namespace App\Helpers;
|
|
|
|
use Morilog\Jalali\Jalalian;
|
|
|
|
class JalaliHelper
|
|
{
|
|
/**
|
|
* Convert a Gregorian date to Jalali.
|
|
*/
|
|
public static function toJalali(int $year, int $month, int $day): array
|
|
{
|
|
return Jalalian::fromGregorian($year, $month, $day)->toArray();
|
|
}
|
|
|
|
/**
|
|
* Convert a Jalali date to Gregorian.
|
|
*/
|
|
public static function toGregorian(int $year, int $month, int $day): array
|
|
{
|
|
return (new Jalalian($year, $month, $day))->toGregorian()->toArray();
|
|
}
|
|
|
|
/**
|
|
* Format a Gregorian date string as Jalali.
|
|
*/
|
|
public static function formatJalali(string $date, string $format = 'Y/m/d'): string
|
|
{
|
|
if (empty($date)) {
|
|
return '';
|
|
}
|
|
|
|
return Jalalian::fromDateTime($date)->format($format);
|
|
}
|
|
|
|
/**
|
|
* Get the Jalali month name.
|
|
*/
|
|
public static function jalaliMonthName(int $month): string
|
|
{
|
|
$months = [
|
|
1 => 'فروردین',
|
|
2 => 'اردیبهشت',
|
|
3 => 'خرداد',
|
|
4 => 'تیر',
|
|
5 => 'مرداد',
|
|
6 => 'شهریور',
|
|
7 => 'مهر',
|
|
8 => 'آبان',
|
|
9 => 'آذر',
|
|
10 => 'دی',
|
|
11 => 'بهمن',
|
|
12 => 'اسفند',
|
|
];
|
|
|
|
return $months[$month] ?? '';
|
|
}
|
|
|
|
/**
|
|
* Get the Jalali day (week) name.
|
|
*/
|
|
public static function jalaliDayName(int $dayOfWeek): string
|
|
{
|
|
$days = [
|
|
0 => 'یکشنبه',
|
|
1 => 'دوشنبه',
|
|
2 => 'سهشنبه',
|
|
3 => 'چهارشنبه',
|
|
4 => 'پنجشنبه',
|
|
5 => 'جمعه',
|
|
6 => 'شنبه',
|
|
];
|
|
|
|
return $days[$dayOfWeek] ?? '';
|
|
}
|
|
|
|
/**
|
|
* Get today's date in Jalali as a formatted string.
|
|
*/
|
|
public static function todayJalali(string $format = 'Y/m/d'): string
|
|
{
|
|
return Jalalian::now()->format($format);
|
|
}
|
|
|
|
/**
|
|
* Format a date for display based on the user's calendar preference.
|
|
*/
|
|
public static function formatDateForDisplay(?string $date, string $calendar = 'jalali', string $format = 'Y/m/d'): string
|
|
{
|
|
if (empty($date)) {
|
|
return '';
|
|
}
|
|
|
|
if ($calendar === 'jalali') {
|
|
return static::formatJalali($date, $format);
|
|
}
|
|
|
|
return date($format, strtotime($date));
|
|
}
|
|
|
|
/**
|
|
* Get the start and end dates for a given Jalali month as Gregorian Carbon instances.
|
|
* Useful for querying records within a specific Jalali month.
|
|
*/
|
|
public static function getQueryRangeForJalaliMonth(int $year, int $month): array
|
|
{
|
|
$start = (new Jalalian($year, $month, 1))->toCarbon();
|
|
$end = (new Jalalian($year, $month, $start->format('t')))->toCarbon()->endOfDay();
|
|
|
|
// Jalalian doesn't have daysInMonth directly, calculate it
|
|
$jalaliEndDay = 29;
|
|
if ($month <= 6) {
|
|
$jalaliEndDay = 31;
|
|
} elseif ($month <= 11) {
|
|
$jalaliEndDay = 30;
|
|
} else {
|
|
// Esfand - check for leap year
|
|
$j = new Jalalian($year, 1, 1);
|
|
$jalaliEndDay = $j->isLeapYear() ? 30 : 29;
|
|
}
|
|
|
|
$start = (new Jalalian($year, $month, 1))->toCarbon()->startOfDay();
|
|
$end = (new Jalalian($year, $month, $jalaliEndDay))->toCarbon()->endOfDay();
|
|
|
|
return [$start, $end];
|
|
}
|
|
}
|