Convert date and time to Jalali in Laravel
Asked Answered
I

8

13

I want to change the time and date calendar to Jalali (Shamsi calendar used in Iran) in my system. How can I do this?

"language_title" => "English",
"dir" => "ltr", // ltr = left to right, rtl = right to left
"language_code" => "en",
"intl_locale" => "en_US",
"dec_point" => ".",
"thousands_sep" => ",",
"dateformat_full" => "D, M j Y  g:i A", // http://php.net/manual/en/function.date.php
"dateformat_date_long" => "D, M j Y", // http://php.net/manual/en/function.date.php
"intl_dateformat_full" => "E, d MMM yy  h:m a", // http://userguide.icu-project.org/formatparse/datetime
"intl_dateformat_date_long" => "E, d MMMM yyyy", // http://userguide.icu-project.org/formatparse/datetime
Insnare answered 21/5, 2015 at 15:21 Comment(7)
You want just to change the time zone of your application ??Terceira
Have a look at the Carbon library. It's an extension to the standard DateTime object in PHP that adds some localization options.Azotobacter
@maraboc yes,i want to change time zone :)Insnare
@kryten do you know a simple way?:)Insnare
@HamidrezaGhaderi Carbon is as simple as it gets. Plus, it is very useful. Spending 30 minutes in learning it will do you much good in the future. Also, your question isn't necessarily related to Laravel.Scatterbrain
Did you change it in th config/app.php file ??Terceira
@maraboc i want to change calnders to Jalali Calendar :).i am confusedInsnare
H
8

You can use Jalali/Morilog.

To Convert Jalali Date To Georgian Carbon Date:

$jalaliDate = "1399/08/06";
$georgianCarbonDate=\Morilog\Jalali\Jalalian::fromFormat('Y/m/d', $jalaliDate)->toCarbon();

and to Convert Georgian Carbon Date to Jalali Date:

$jalaliDate=\Morilog\Jalali\Jalalian::fromCarbon($carbonDate)->format('Y/m/d'); // output is a jalali date string like 1399/08/06
Honeybunch answered 6/9, 2020 at 4:46 Comment(0)
H
6

Many years ago, I made a third party calendar helper to convert a Gregorian date to Jalali. I use it inside a Laravel service. The main methods are jNow and jdate

<?php

namespace Infrastructure\Services\Calendar;

class CalendarHelper
{

    public function jDisplay($timestamp)
    {
        list($hour, $minute, $second) = explode( ':', date( 'H:i:s', time() ) );
        $today = time() - ( $hour*3600+$minute*60+$second );
        if ( $timestamp > $today ) return 'امروز';
        elseif ( $timestamp > $today-86400 ) return 'دیروز';
        else return $this->jdate( $timestamp, false, true );
    }

    public function yearList($end=1970)
    {
        $year = (int)date('Y', time());
        return $this->_yearList($year, $end);
    }

    private function _yearList($year, $end)
    {
        if ( $year < $end ) return array();

        $years[] = $year;
        while ( $year > $end ) {
            $year--;
            $years[] = $year;
        }
        return $years;
    }

    public function jYearList($end=1340)
    {
        $list = explode(' ', $this->jdate( date('Y-m-d-D H:i:s', time()), true, false ) );
        $year = (int)$list[3];
        return $this->_yearList($year, $end);
    }

    public function jNow()
    {
        $list = explode(' ', $this->jdate( date('Y-m-d-D H:i:s', time()), true, false ) );
        return array(
            'year' => $list[3],
            'month' => $list[2],
            'day' => $list[1],
            'dayofweek' => $list[0],
            'time' => $list[4]
        );
    }

    public function gTime($date, $hour, $minute)
    {
        list($j_y,$j_m,$j_d) = explode('/', $date);
        list($g_y, $g_m, $g_d) = $this->jalali_to_gregorian($j_y,$j_m,$j_d);
        $hour = str_pad( $hour, 2, '0', STR_PAD_LEFT );
        $minute = str_pad( $minute, 2, '0', STR_PAD_LEFT );
        $time_str = $g_y.'-'.$g_m.'-'.$g_d.' '.$hour.':'.$minute.':00';
        $time = strtotime($time_str);
        $time -= 12600;
        return $time;
    }

    public function jdate($datetime, $hastime=false, $localize=true)
    {
        list ($date,$time) = explode (' ', $datetime);
        $dateArray = explode ('-', $date);
        if ( count($dateArray) == 4 ) {
            list ($g_y, $g_m, $g_d,$g_w) = $dateArray;
        } else {
            list ($g_y, $g_m, $g_d) = $dateArray;
            $g_w = '';
            $jw = '';
        }
        $jy=$g_y;
        $i=$g_m-1;
        $j_day_no=$g_d-1;
        if($g_y>1600)
        {
            $g_days_in_month = array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
            $j_days_in_month = array(31, 31, 31, 31, 31, 31, 30, 30, 30, 30, 30, 29);

            $div = create_function('$a,$b','return (int) ($a / $b);');

            $gy = $g_y-1600;
            $gm = $g_m-1;
            $gd = $g_d-1;
            $g_day_no = 365*$gy+$div($gy+3,4)-$div($gy+99,100)+$div($gy+399,400);

            for ($i=0; $i < $gm; ++$i)
                $g_day_no += $g_days_in_month[$i];

            if ($gm>1 && (($gy%4==0 && $gy%100!=0) || ($gy%400==0))) {
                /* leap and after Feb */
                $g_day_no++;
            }

            $g_day_no += $gd;

            $j_day_no = $g_day_no-79;

            $j_np = $div($j_day_no, 12053); /* 12053 = 365*33 + 32/4 */
            $j_day_no = $j_day_no % 12053;

            $jy = 979+33*$j_np+4*$div($j_day_no,1461); /* 1461 = 365*4 + 4/4 */
            $j_day_no %= 1461;

            if ($j_day_no >= 366)
                {
                    $jy += $div($j_day_no-1, 365);
                    $j_day_no = ($j_day_no-1)%365;
            }

            for ($i = 0; $i < 11 && $j_day_no >= $j_days_in_month[$i]; ++$i)
                $j_day_no -= $j_days_in_month[$i];
        }

            if ($localize == false )
                $jm = $i+1;
            else
            switch($i){
                case 0:
                    $jm="فروردین";
                    break;
                case 1:
                    $jm="اردیبهشت";
                    break;
                case 2:
                    $jm="خرداد";
                    break;
                case 3:
                    $jm="تیر";
                    break;
                case 4:
                    $jm="مرداد";
                    break;
                case 5:
                    $jm="شهریور";
                    break;
                case 6:
                    $jm="مهر";
                    break;
                case 7:
                    $jm="آبان";
                    break;
                case 8:
                    $jm="آذر";
                    break;
                case 9:
                    $jm="دی";
                    break;
                case 10:
                    $jm="بهمن";
                    break;
                case 11:
                    $jm="اسفند";
                    break;
            };
        $jd = $j_day_no+1;
            switch($g_w){
                case "Sat":
                    $jw="&#1588;&#1606;&#1576;&#1607;";
                    break;
                case "Sun":
                    $jw="&#1610;&#1603;&#8204;&#1588;&#1606;&#1576;&#1607;";
                    break;
                case "Mon":
                    $jw="&#1583;&#1608;&#1588;&#1606;&#1576;&#1607;";
                    break;
                case "Tue":
                    $jw="&#1587;&#1607;&#8204;&#1588;&#1606;&#1576;&#1607;";
                    break;
                case "Wed":
                    $jw="&#1670;&#1607;&#1575;&#1585;&#1588;&#1606;&#1576;&#1607;";
                    break;
                case "Thu":
                    $jw="&#1662;&#1606;&#1580;&#8204;&#1588;&#1606;&#1576;&#1607;";
                    break;
                case "Fri":
                    $jw="&#1580;&#1605;&#1593;&#1607;";
                    break;
            };

        $time = $hastime == false ? '' : $time;

        return "$jw $jd $jm $jy $time";
    }

    function div($a,$b) {
        return (int) ($a / $b);
    }

    function jalali_to_gregorian($j_y, $j_m, $j_d)
    {
        $g_days_in_month = array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
        $j_days_in_month = array(31, 31, 31, 31, 31, 31, 30, 30, 30, 30, 30, 29);



        $jy = $j_y-979;
        $jm = $j_m-1;
        $jd = $j_d-1;

        $j_day_no = 365*$jy + $this->div($jy, 33)*8 + $this->div($jy%33+3, 4);
        for ($i=0; $i < $jm; ++$i)
        $j_day_no += $j_days_in_month[$i];

        $j_day_no += $jd;

        $g_day_no = $j_day_no+79;

        $gy = 1600 + 400*$this->div($g_day_no, 146097); /* 146097 = 365*400 + 400/4 - 400/100 + 400/400 */
        $g_day_no = $g_day_no % 146097;

        $leap = true;
        if ($g_day_no >= 36525) /* 36525 = 365*100 + 100/4 */
        {
            $g_day_no--;
            $gy += 100*$this->div($g_day_no,  36524); /* 36524 = 365*100 + 100/4 - 100/100 */
            $g_day_no = $g_day_no % 36524;

            if ($g_day_no >= 365)
            $g_day_no++;
            else
            $leap = false;
        }

        $gy += 4*$this->div($g_day_no, 1461); /* 1461 = 365*4 + 4/4 */
        $g_day_no %= 1461;

        if ($g_day_no >= 366) {
            $leap = false;

            $g_day_no--;
            $gy += $this->div($g_day_no, 365);
            $g_day_no = $g_day_no % 365;
        }

        for ($i = 0; $g_day_no >= $g_days_in_month[$i] + ($i == 1 && $leap); $i++)
        $g_day_no -= $g_days_in_month[$i] + ($i == 1 && $leap);
        $gm = $i+1;
        $gd = $g_day_no+1;

        return array($gy, $gm, $gd);
    }

    function gregorian_to_jalali($g_y, $g_m, $g_d)
    {
        $d_4 = $g_y % 4;
        $g_a = array(0, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334);
        $doy_g = $g_a[(int)$g_m] + $g_d;
        if ($d_4 == 0 and $g_m > 2)
            $doy_g++;
        $d_33 = (int)((($g_y - 16) % 132) * 0.0305);
        $a = ($d_33 == 3 or $d_33 < ($d_4 - 1) or $d_4 == 0) ? 286 : 287;
        $b = (($d_33 == 1 or $d_33 == 2) and ($d_33 == $d_4 or $d_4 == 1)) ? 78 : (($d_33 == 3 and $d_4 == 0) ? 80 : 79);
        if((int)(($g_y - 10) / 63) == 30) {
            $a--;
            $b++;
        }
        if($doy_g > $b) {
            $jy = $g_y - 621;
            $doy_j = $doy_g - $b;
        }
        else {
            $jy = $g_y - 622;
            $doy_j = $doy_g + $a;
        }
        if($doy_j < 187) {
            $jm = (int)(($doy_j - 1) / 31);
            $jd = $doy_j - (31 * $jm++);
        }
        else {
            $jm = (int)(($doy_j - 187) / 30);
            $jd = $doy_j - 186 - ($jm * 30);
            $jm += 7;
        }
        return array($jy, $jm, $jd);
    }

}

/* End of file Calendar.php */
Hemphill answered 27/3, 2018 at 22:45 Comment(1)
You can also use this packageHemphill
S
1

in config/app.php set timezone to 'Asia/Tehran' but it saves only time based on iran for converting Gregorian to Shamsi Date you can use Miladr Package

Skirmish answered 18/5, 2016 at 6:15 Comment(0)
S
1

You Can Use These Two Packages

Verta

composer require hekmatinasser/verta

Example

Verta::jalaliToGregorian(1401,03,27);

// output [2020,6,17]

OR

morilog/jalali php >= 7.0

composer require morilog/jalali:3.* 

Example

public function toCarbon(): Carbon

$date = (new Jalalian(1397, 1, 18, 12, 10, 0))
      ->toCarbon()
      ->toDateTimeString(); 

 // output: 2018-04-07 12:10:00
Slangy answered 25/12, 2022 at 11:42 Comment(0)
T
0

In your config/app.php file, you've to change the following setting :

/*
|--------------------------------------------------------------------------
| Application Timezone
|--------------------------------------------------------------------------
|
| Here you may specify the default timezone for your application, which
| will be used by the PHP date and date-time functions. We have gone
| ahead and set this to a sensible default for you out of the box.
|
*/

'timezone' => 'IST',

Assuming that your timezone is IST :)

Terceira answered 21/5, 2015 at 16:19 Comment(1)
What is your time zone ?Terceira
V
0
$wrongFormat = 1360-1-1;
$correctFormat = 1360-01-01;

You can use like this:

$correctDate = date('Y-m-d',strtotime($wrongFormat));
$miladiDate = \Morilog\Jalali\CalendarUtils::createCarbonFromFormat('Y-m-d',$correctDate);
Vereen answered 21/9, 2021 at 6:4 Comment(0)
H
0

you can use morilog/jalali package.

1- Run the Composer update command

    composer require morilog/jalali:3.*

2- Add name space

 use Morilog\Jalali\Jalalian;

3- Basic Usage

  • Convert Jalali to Carbon

     $jDate = "1400/07/05";
     $date = Jalalian::fromFormat('Y-m-d', $date)->toCarbon();
    
  • Convert Carbon to Jalali

     $date = '2020/09/27';
     $jDate = Jalalian::fromCarbon($date)->format('Y-m-d');
     // or
     $jDate = jdate($date)->format('Y-m-d');
    
    

also u can use Y-m-d H:i:s or H:i:s as format.

Heavyhearted answered 27/9, 2021 at 11:44 Comment(0)
B
0

You can use Jdf library and call it easily

Baseburner answered 8/7, 2023 at 9:38 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Lampion

© 2022 - 2024 — McMap. All rights reserved.