I want to use PHP's IntlDateFormater to use persian calendar in my code. This days PHP has built-in functions witch supoorts persian calendar and works fine but I have a problem with certain days.
for example using this code all dates converts from gregorian to persian until date 2018-3-17 which converts to "1396-12-26" and after that, the date 2018-3-18 converts to "1397-12-27" which i dont know why the year is increased?
my php code:
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 'Next mpnth:'." <br>\n";
$d->modify('+26 day');
$d->setCalendar('gregorian');
echo $d->format('Y-M-d');
$d->setCalendar('persian');
echo " Persian: ".$d->format('Y-M-d')."<br >\n";
echo 'Next day:'." <br>\n";
$d->modify('+1 day');
$d->setCalendar('gregorian');
echo $d->format('Y-M-d');
$d->setCalendar('persian');
echo " Persian: ".$d->format('Y-M-d')."<br >\n";
The output:
2018-2-19 Persian: 1396-11-30
Next mpnth:
2018-3-17 Persian: 1396-12-26
Next day:
2018-3-18 Persian: 1397-12-27