在 wordpress 中,我已经将格里高利历日期正确转换为波斯语,但我的代码以英语(英式英语)返回 perisan 月,例如它返回 Farvardin 而不是 فروردین 和其他 11 个月。
到目前为止这是我的代码。
$now = new DateTime();
$formatterMonth = new IntlDateFormatter(
"en_US@calendar=persian",
IntlDateFormatter::FULL,
IntlDateFormatter::FULL,
'Asia/Tehran',
IntlDateFormatter::TRADITIONAL,
"dd MMMM",
);
echo $formatterMonth->format($now); //output ۲۹ Farvardin
要将公历日期转换为 Shamsi(Jalali),最好使用 JDF 或 Morilog 库
Morilog 库: https://github.com/morilog/jalali/
但是如果你仍然想使用自己的代码,你可以轻松地将 finglish 月份转换为 farsi:
<?php
$now = new DateTime();
$formatterMonth = new IntlDateFormatter(
"en_US@calendar=persian",
IntlDateFormatter::FULL,
IntlDateFormatter::FULL,
'Asia/Tehran',
IntlDateFormatter::TRADITIONAL,
"dd MMMM",
);
$myDate = $formatterMonth->format($now); //output ۲۹ Farvardin
$monthArray = [
'Farvardin' => 'فروردین',
'Ordibehesht' => 'اردیبهشت',
'Khordad' => 'خرداد',
'Tir' => 'تیر',
'Mordad' => 'مرداد',
'Shahrivar' => 'شهریور',
'Mehr' => 'مهر',
'Aban' => 'آبان',
'Azar' => 'آذر',
'Dey' => 'دی',
'Bahman' => 'بهمن',
'Esfand' => 'اسفند',
];
$explodedDate = explode(" ",$myDate);
$myFinglishMonth = $explodedDate[1];
$myFarsiMonth = $monthArray[$myFinglishMonth];
$convertedFarsiDate = $explodedDate[0]." ".$myFarsiMonth;
echo $convertedFarsiDate;
希望对你有帮助
Kamyar 你的回答是对的,但我找到了更短的方法来做到这一点。
function getDateIntl(?\DateTime $date = null, ?string $locale = null, ?DateTimeZone
$timezone = null, $dateFormat) {
$date = $date ?? new \DateTime();
$locale = $locale ?? \Locale::getDefault();
$formatter = new \IntlDateFormatter(
$locale,
IntlDateFormatter::FULL,
IntlDateFormatter::FULL,
$timezone,
IntlDateFormatter::TRADITIONAL,
$dateFormat
);
return $formatter->format($date);
}
$persianDateRight = getDateIntl(new DateTime(), "fa@calendar=persian", new \DateTimeZone('Asia/Tehran'), 'eeee، dd MMMM');
这样就不需要在数组内部循环了。并且还可以更改为任何语言和时区以及时间格式