Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

zxywvu's avatar

Undefined array key "year"

helpers.php

<?php

use Morilog\Jalali\CalendarUtils;

if (! function_exists('jalalitomiladi')) {
    function jalalitomiladi($date)
    {
        $year = intval(substr($date, 0, 4));
        $month = intval(substr($date, 5, 2));
        $day = intval(substr($date, 8, 2));
        return CalendarUtils::createDatetimeFromFormat("$year-$month-$day", $date);
    }
}

Controller.php

public function store(Request $request)
{
    $xxx = jalalitomiladi($request->input('date'));
    dd($xxx);
0 likes
31 replies
Sinnbeck's avatar

What line throws that error? Some code you aren't showing?

Sinnbeck's avatar

@esfer the error comes from the class that you are passing it to. Maybe it expects an array

You can share the actual error from that page so we can see more

zxywvu's avatar

@Sinnbeck It is not an array

public static function createDatetimeFromFormat($format, $str, $timezone = null)
{
    $pd = self::parseFromFormat($format, $str);
    $gd = self::toGregorian($pd['year'], $pd['month'], $pd['day']);
    $date = self::createDateTime('now', $timezone);
    $date->setDate($gd[0], $gd[1], $gd[2]);
    $date->setTime($pd['hour'], $pd['minute'], $pd['second']);

    return $date;
}
Sinnbeck's avatar

@esfer so it seems that this line isn't able to extract a year

$pd = self::parseFromFormat($format, $str);

And you haven't shown the helper function

zxywvu's avatar

@Sinnbeck I want to convert the Jalali date to Gregorian and then store it in the database.

Sinnbeck's avatar

@esfer well without seeing the code involved its close to impossible. My guess is that your own helper function is wrong

psrz's avatar

I don't think you're passing that first parameter to CalendarUtils::createDatetimeFromFormat("$year-$month-$day", $date); correctly

Assuming the original value of $date' is correct, for example '2022-08-07', then the value of that first parameter will be "2022-8-7" and not something like Y-m-d

"2022-8-7" is the result of "$year-$month-$day"

kokoshneta's avatar

@psrz Edit: Just realised I misread your answer completely. You are of course right that it will never work the way it’s set up now.

@esfer The first argument should be how the date is formatted, not the date itself. That’s very clearly shown in the example on the GitHub page:

use Morilog\Jalali\CalendarUtils;

if (! function_exists('jalalitomiladi')) {
    function jalalitomiladi($date)
    {
        return CalendarUtils::createDatetimeFromFormat("Y-m-d", $date);
    }
}
psrz's avatar

@esfer I would get rid of the helper function since it doesn't seem to be doing much. In your controller I would try this:

 $request->validate([
	'date' => ['required', 'date_format:Y-m-d'],
 ]);

 $jalali_date = CalendarUtils::createDatetimeFromFormat('Y-m-d', $request->date);

You should always validate the input on requests.

psrz's avatar

@esfer well, then you need to send the value in the rigth format

what does dd($request->toArray()); show ?

Sinnbeck's avatar

@esfer yes? The error is in right there. The date you send in is the wrong format

psrz's avatar

@esfer There you go. Make both the input and the rule have the same format.

kokoshneta's avatar

@esfer Where are you actually getting your dates from? Is it a date picker? Can the user just type in what they want in a text field? Does it come from something stored in a database (in whatever format)?

Most importantly: the date you showed here is in the format Y/m/d, not Y-m-dare you sure it will always arrive in that format?

If the format is liable to change, you’re pretty much out of luck – systems made to guess date formats from a string are geared to the Gregorian calendar, and I doubt they can guess a Jalali date. The Jalali package you’re using always requires a format (Y-m-d, Y/m/d, d.m.Y or any other possible variation).

If you know the dates will always arrive in the same format, you just need to use that same format when using the Jalali package. If your input date is 1401/05/16, you would do this:

$jalali_date = CalendarUtils::createDatetimeFromFormat('Y/m/d', $request->date);
zxywvu's avatar

@Sinnbeck I tried to solve it

public function store(Request $request)
{
    dd($request->date);

demo

^ "1401-05-16"

Snapey's avatar

100% oxbir or his brother

2 likes

Please or to participate in this conversation.