在 Laravel 中将日期和时间转换为 Jalali
Posted
技术标签:
【中文标题】在 Laravel 中将日期和时间转换为 Jalali【英文标题】:Convert date and time to Jalali in Laravel 【发布时间】:2015-08-03 08:52:23 【问题描述】:我想将时间和日期日历更改为Jalali(伊朗使用的 Shamsi 日历) 在我的系统中。 我该怎么做?
"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
【问题讨论】:
您只想更改应用程序的时区?? 看看Carbon 库。它是 PHP 中标准DateTime
对象的扩展,添加了一些本地化选项。
@maraboc 是的,我想更改时区 :)
@kryten 你知道一个简单的方法吗?:)
@HamidrezaGhaderi Carbon 非常简单。另外,它非常有用。花 30 分钟学习它会对你未来有很大的帮助。另外,您的问题不一定与 Laravel 有关。
【参考方案1】:
您可以使用morilog/jalali 包。
1- 运行 Composer 更新命令
composer require morilog/jalali:3.*
2- 添加命名空间
use Morilog\Jalali\Jalalian;
3- 基本用法
将 Jalali 转换为碳
$jDate = "1400/07/05";
$date = Jalalian::fromFormat('Y-m-d', $date)->toCarbon();
将 Carbon 转换为 Jalali
$date = '2020/09/27';
$jDate = Jalalian::fromCarbon($date)->format('Y-m-d');
// or
$jDate = jdate($date)->format('Y-m-d');
您也可以使用Y-m-d H:i:s
或 H:i:s
作为格式。
【讨论】:
【参考方案2】:$wrongFormat = 1360-1-1;
$correctFormat = 1360-01-01;
你可以这样使用:
$correctDate = date('Y-m-d',strtotime($wrongFormat));
$miladiDate = \Morilog\Jalali\CalendarUtils::createCarbonFromFormat('Y-m-d',$correctDate);
【讨论】:
【参考方案3】:您可以使用Jalali/Morilog。
将波斯日期转换为碳日期:
pdate = "1399/08/06";
\Morilog\Jalali\Jalalian::fromFormat('Y/m/d', $pdate)->toCarbon();
并将碳日期转换为波斯日期:
\Morilog\Jalali\Jalalian::fromCarbon($carbon_date)->format('Y/m/d'); // output is a persian date string like 1399/08/06
【讨论】:
【参考方案4】:许多年前,我制作了一个第三方日历助手来将公历日期转换为 Jalali。我在 Laravel 服务中使用它。主要方法有jNow
和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="شنبه";
break;
case "Sun":
$jw="يك‌شنبه";
break;
case "Mon":
$jw="دوشنبه";
break;
case "Tue":
$jw="سه‌شنبه";
break;
case "Wed":
$jw="چهارشنبه";
break;
case "Thu":
$jw="پنج‌شنبه";
break;
case "Fri":
$jw="جمعه";
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 */
【讨论】:
你也可以用这个package【参考方案5】:在config/app.php
中将时区设置为“亚洲/德黑兰”,但它只节省了基于伊朗将公历转换为 Shamsi 日期的时间,您可以使用Miladr Package
【讨论】:
【参考方案6】:从西方(公历)日历转换为另一个日历会很困难。一个快速的谷歌搜索想出了一个项目:
persian_date - 用于在公历和波斯历之间转换日期的 PHP 库。请注意,我没有尝试过使用这个库,我什至不确定它会做你想做的事。
【讨论】:
【参考方案7】:在您的 config/app.php
文件中,您必须更改以下设置:
/*
|--------------------------------------------------------------------------
| 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',
假设您的时区是IST
:)
【讨论】:
你的时区是什么?以上是关于在 Laravel 中将日期和时间转换为 Jalali的主要内容,如果未能解决你的问题,请参考以下文章
在 php 中将 datepicker 字符串转换为简单的日期格式