What line throws that error? Some code you aren't showing?
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);
@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
@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;
}
@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
@Sinnbeck How can I get an output?
@esfer don't know what you mean?
@Sinnbeck I want to convert the Jalali date to Gregorian and then store it in the database.
@Sinnbeck Where are you? Can you help me?
@esfer well without seeing the code involved its close to impossible. My guess is that your own helper function is wrong
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"
@psrz what's the solution?
@psrz I installed https://github.com/morilog/jalali package
@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);
}
}
@esfer not helpful. The error is in the helper function
@Sinnbeck 😭😭😭😭😭😭😭😭😭 WHY?
@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.
@esfer read my comment? Just curious. Are you oxbir?
@Sinnbeck No , I am not oxbir
@esfer well, then you need to send the value in the rigth format
what does dd($request->toArray()); show ?
@esfer yes? The error is in right there. The date you send in is the wrong format
@esfer can you spot the problem?
@esfer There you go. Make both the input and the rule have the same format.
@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-d – are 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);
@Sinnbeck I tried to solve it
public function store(Request $request)
{
dd($request->date);
demo
^ "1401-05-16"
100% oxbir or his brother
@Snapey It's definitely Oxbir.
Please or to participate in this conversation.