vernova/app/Helpers/CalendarHelper.php
2026-07-26 19:58:27 +03:30

139 lines
4.1 KiB
PHP

<?php
namespace App\Helpers;
class CalendarHelper
{
/**
* Parse a date string (may be Jalali) and return a Gregorian date string.
* If the calendar preference is 'jalali', try to convert from Jalali format.
*/
public static function parseDate(?string $date): ?string
{
if (empty($date)) {
return null;
}
// If it's already a valid Gregorian date (Y-m-d), return as-is
if (preg_match('/^\d{4}-\d{2}-\d{2}$/', $date)) {
return $date;
}
// Try to parse as Jalali date (Y/m/d or Y-m-d with Jalali year)
$calendar = session('calendar', auth()->user()?->preferred_calendar ?? 'jalali');
if ($calendar === 'jalali' && class_exists(\Morilog\Jalali\Jalalian::class)) {
try {
// Try Jalali format: 1404/03/15 or 1404-03-15
$normalized = str_replace('/', '-', $date);
$parts = explode('-', $normalized);
if (count($parts) === 3) {
$jYear = (int) $parts[0];
$jMonth = (int) $parts[1];
$jDay = (int) $parts[2];
// If year looks like a Jalali year (> 1300), convert
if ($jYear > 1300 && $jYear < 1600) {
$gregorian = \Morilog\Jalali\Jalalian::fromDateTime(
\Morilog\Jalali\CalendarUtils::toGregorian($jYear, $jMonth, $jDay)
);
return $gregorian->format('Y-m-d');
}
}
} catch (\Exception $e) {
// Fall through to Carbon parse
}
}
// Fallback: try Carbon parse
try {
return \Carbon\Carbon::parse($date)->format('Y-m-d');
} catch (\Exception $e) {
return $date;
}
}
/**
* Format a date based on the user's calendar preference.
*/
public static function formatDate(?string $date, string $calendar = 'jalali', string $format = 'Y/m/d'): string
{
return JalaliHelper::formatDateForDisplay($date, $calendar, $format);
}
/**
* Get the month name for a given month number.
*/
public static function getMonthName(int $month, string $calendar = 'jalali'): string
{
if ($calendar === 'jalali') {
return JalaliHelper::jalaliMonthName($month);
}
$months = [
1 => 'January',
2 => 'February',
3 => 'March',
4 => 'April',
5 => 'May',
6 => 'June',
7 => 'July',
8 => 'August',
9 => 'September',
10 => 'October',
11 => 'November',
12 => 'December',
];
return $months[$month] ?? '';
}
/**
* Get the day (of week) name.
*/
public static function getDayName(int $dayOfWeek, string $calendar = 'jalali'): string
{
if ($calendar === 'jalali') {
return JalaliHelper::jalaliDayName($dayOfWeek);
}
$days = [
0 => 'Sunday',
1 => 'Monday',
2 => 'Tuesday',
3 => 'Wednesday',
4 => 'Thursday',
5 => 'Friday',
6 => 'Saturday',
];
return $days[$dayOfWeek] ?? '';
}
/**
* Get the current date formatted based on the calendar preference.
*/
public static function getCurrentDate(string $calendar = 'jalali', string $format = 'Y/m/d'): string
{
return self::formatDate(now()->format('Y-m-d'), $calendar, $format);
}
/**
* Get the query date range for a given month and year.
*/
public static function getQueryRangeForMonth(int $year, int $month, string $calendar = 'jalali'): array
{
if ($calendar === 'jalali') {
return JalaliHelper::getQueryRangeForJalaliMonth($year, $month);
}
// Gregorian range
$start = \Carbon\Carbon::create($year, $month, 1)->startOfDay();
$end = \Carbon\Carbon::create($year, $month, 1)->endOfMonth()->endOfDay();
return [$start, $end];
}
}