我想使用PHP的IntlDateFormater在代码中使用波斯日历。现在,PHP具有内置功能,可以正常工作,但是我在某些日子有问题。
例如,使用此代码,所有日期都会从公历转换为波斯语,直到日期2018-3-17转换为“ 1396-12-26”,之后,日期2018-3-18转换为“ 1397-12-27”,我不知道为什么年份增加了?
我的php代码:
class MyDateTime extends \DateTime {
protected $calendar='gregorian';
protected $timezone;
public function __construct() {
$timezone = new \DateTimeZone('Asia/Tehran');
$this->timezone = $timezone;
parent::__construct(null, $timezone);
$this->setCalendar('gregorian');
$this->modify('2018-2-19');
}
public function setCalendar($calendar) {
$this->calendar = strtolower($calendar);
return $this;
}
public function format($pattern){
$formater = new \IntlDateFormatter('en_US' . '@calendar=' . $this->calendar,
\IntlDateFormatter::FULL, \IntlDateFormatter::FULL, $this->timezone,
$this->calendar == 'gregorian' ? \IntlDateFormatter::GREGORIAN : \IntlDateFormatter::TRADITIONAL, $pattern);
return $formater->format(parent::format('U'));
}
}
$d = new MyDateTime();
$d->setCalendar('gregorian');
echo $d->format('Y-M-d');
$d->setCalendar('persian');
echo " Persian: ".$d->format('Y-M-d')."<br >\n";
echo …Run Code Online (Sandbox Code Playgroud)