Create Julian date from Carbon with Jalali in laravel

675 Views Asked by At

I have a expiration date for my file in database.

I want to convert it to julian persian date and compact it to the edit page.

This is my Controller:

Public function edit(Panelfile $panelfile)
{

$expiration = Jalalian::fromCarbon($panelfile->expiration)->format('Y-m-d');

$date = CalendarUtils::toJalali($expiration);

return view('file.edit',compact('panelfile','date'));
}

this is my datepicker input:

<div class="form-group">
<input type="text" name"expiration" value="{{$date}}" data-jdp>
</div>

I get this error when I go to the edit page:

Agrument 1 passed to Morilog\Jalali\Jalalian::fromCarbon() must be an intance of Carbon\Carbon, string given

How can i solve this?

1

There are 1 best solutions below

2
Rouhollah Mazarei On

Change this line:

$expiration = Jalalian::fromCarbon($panelfile->expiration)->format('Y-m-d');

to this:

$expiration = Jalalian::fromCarbon(Carbon::parse($panelfile->expiration))->format('Y-m-d');

Now you have something like 1400-06-02 in $expiration (it's Jalali date).

If you take a look at toJalali method, it takes 3 argument:

/**
 * Converts a Gregorian date to Jalali.
 *
 * @param $gy
 * @param $gm
 * @param $gd
 * @return array
 * 0: Year
 * 1: Month
 * 2: Day
 */
public static function toJalali($gy, $gm, $gd)
{
    return self::d2j(self::g2d($gy, $gm, $gd));
}

You already have the Jalali date and (I think) you don't need to use the line after that. But if you want to use toJalali method, you should pass three arguments to it:

  1. Gregorian year (for example: 2021)
  2. Gregorian month (for example: 8)
  3. Gregorian day (for example: 24)