5

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
mehdi jalilvand
  • 450
  • 4
  • 12

1 Answers1

1

Don't know why your code not work. A old PHP-Version? This work fine with PHP 7.2.21:

function dateGregorianToPersian($strDate)
{
  $tz = new DateTimeZone('Asia/Tehran');
  $fmt = datefmt_create(
    "ir_IR@calendar=persian", 
    IntlDateFormatter::FULL, 
    IntlDateFormatter::FULL, 
    $tz, 
    IntlDateFormatter::TRADITIONAL,
    "yyyy-MM-dd"
  );
  return datefmt_format( $fmt ,date_create($strDate,$tz));
}

//test
$data = ['2018-2-19','2018-3-17','2018-3-18'];
foreach($data as $strDate){
  echo $strDate.' Persian: '.dateGregorianToPersian($strDate).'<br>';
}

Output

2018-2-19 Persian: 1396-11-30
2018-3-17 Persian: 1396-12-26
2018-3-18 Persian: 1396-12-27
jspit
  • 7,276
  • 1
  • 9
  • 17