Search code examples
laraveldatepickerjalali-calendar

Undefned index year for Jalali calender in Laravel


I want to save date in database. I'm using Persian_datepicker package for choosing date and also using Jalali calendar for converting to gregorian date. But I got the error:

Error:

Undefned index year

Code:

pdate = "۱۴۰۰/۰۳/۲۹";
\Morilog\Jalali\Jalalian::fromFormat('Y/m/d', $pdate)->toCarbon();

Solution

  • You should convert Persian date to English format and then convert to Gregorian.

    ۱۴۰۰-۰۳-۲۹ (Persian format) -> 1400-03-29 (English format) -> 2021-06-19 (gregorian)

    I used CalendarUtils class from Jalali package. Suppose $date is "۱۴۰۰-۰۳-۲۹".

    Code:

    public function jalaliToCarbon($date)
        {
            $date = CalendarUtils::convertNumbers($date, true);
            $latin_date = CalendarUtils::createCarbonFromFormat('Y-m-d', $date)->format('Y-m-d');
            return $latin_date;
        }