Home > Blockchain >  How to convert timestamp to Persian at Laravel Migration
How to convert timestamp to Persian at Laravel Migration

Time:08-30

I have this GET table and as you can see at the end of it, I have added members.created_at to return the date that user is registered on the website:

class CreateRegisteredStudentInfoView extends Migration
{
    public function up()
    {
        \Illuminate\Support\Facades\DB::unprepared("
          drop view if exists getStudentRegisteredInfo;
          create view getStudentRegisteredInfo
               as
            select
            members.mbr_id, members.created_at
            from members
        ");
    }
    ...

And it successfully adds the timestamp.

But I do need to change this timestamp to Jalali (Shamsi or Iranian) timestamp.

And I have already installed the enter image description here

Now my question is, how can I use this jdate in the migration code, so that the timestamp would be converted into Persian.

So how can I do this?

CodePudding user response:

we, can not 'automatically' insert the date in Jalali.

you have to follow this two-step

step:1 -> for converting the timestamp to the Jalali date stored in the database already you have to run a seeder for this.

public function run()
{
    $members = Member::all();
    foreach ($members as $key => $member) {
        $jalali_date = digits2persian(jdate($member->created_at)->format('H:i:s Y/m/d'));
        $member->created_at = $jalali_date;
        $member->save();
    }
}

step:2 -> when you insert data to this table you have to insert the Jalali date to the created_at column.

$jalali_date = digits2persian(jdate('your current date')->format('H:i:s Y/m/d'));
$member->created_at = $jalali_date;

CodePudding user response:

Unfortunately, there are no concrete, reproducible examples of how the datum is available and how the datum is expected (concrete code, no images). Therefore, I can only refer to the IntlDateFormatter class, which is already included in the standard PHP installations, with 2 examples.

$dt = date_create("2018-2-19");
$icuFormat = "yyyy:MM:dd";

$fmt = datefmt_create( "fa_IR@calendar=persian" ,IntlDateFormatter::FULL, IntlDateFormatter::FULL,
     'Europe/Berlin',IntlDateFormatter::TRADITIONAL  ,"yyyy:MM:dd");
echo datefmt_format( $fmt ,$dt);  // ۱۳۹۶:۱۱:۳۰

Try it self on 3v4l.org

$fmt = datefmt_create( "fa_IR@calendar=persian" ,IntlDateFormatter::FULL, IntlDateFormatter::FULL,
     'Europe/Berlin',IntlDateFormatter::GREGORIAN  ,"yyyy:MM:dd");

echo datefmt_format( $fmt ,$dt); //۲۰۱۸:۰۲:۱۹

The icu-formats are documented here.

  • Related