2

I need a function to convert a date in the Persian calendar to its counterpart Gregorian date. I wrote the below function following Example #3 of this page. Unfortunately, it does not work. What is the reason and what is the correct method to do that using the Intl extension?

function convert($time, $fromCalendar = 'gregorian', $toCalender = 'persian', $fromFormat = 'yyyy-MM-dd HH:mm:ss',
                 $toFormat = 'yyyy-MM-dd HH:mm:ss', $timezone = null, $local = 'fa_IR')
{
    $formatter = IntlDateFormatter::create($local . '@calendar:' . $fromCalendar, null, null, $timezone, null, $fromFormat);
    $formatter->setCalendar(IntlCalendar::createInstance(null, $local . '@calendar:' . $toCalender));
    $output = $formatter->format($formatter->parse($time));
    if ($output) return $output;
    return $formatter->getErrorMessage();
}

A good introduction to Intl can be found here: http://devzone.zend.com/1500/internationalization-in-php-53/

I used https://github.com/salarmehr/cosmopolitan to convert from Gregorian to other calendars. The question is converting from other calendars to Gregorian.

Handsome Nerd
  • 17,114
  • 22
  • 95
  • 173

2 Answers2

1

This seems promising: http://www.phpclasses.org/package/4852-PHP-Convert-and-format-dates-of-the-Persian-calendar.html

Then you would just do:

$persian=new persian_date();  
echo $persian->date('y-m-d H:i:s');
Jamie
  • 1,340
  • 11
  • 22
  • 1
    Thanks. But I need to use the features provided by icu-project.org and Persian calender is just of the calender type I need. – Handsome Nerd Jun 05 '14 at 13:21
0

The following code convert a date of a gregorian calendar (western calendar) to a date of foreign calendar and the it will convert the date of a foreign calendar backt to a date of a gregorian calendar. The conversion with the chinese calender show to be buggy.

 <?php
// detect the allowed calendars
$bundle = new ResourceBundle('', 'ICUDATA');
$cnames = [];
$calendars = $bundle->get('calendar');
foreach ($calendars as $n => $v) {
    $cnames[] = $n;
}
echo('allowed calendars'); // https://stackoverflow.com/questions/32540529/get-list-of-calendars-timezones-locales-in-php-icu-intldateformatter
echo print_r($cnames, true);
echo("\n\n");
// test the calendars
foreach ([
             'buddhist',
             'chinese',  // chinese calendar has a bug
             'coptic',
             'dangi',
             'default',
             'ethiopic',
             'ethiopic-amete-alem',
             'gregorian',
             'hebrew',
             'indian',
             'islamic',
             'islamic-civil',
             'japanese',
             'persian',
             'roc',
             'funktioniertnicht',
         ] as $calendar) {
    echo($calendar . "\n");
    $intlDateFormatterFromGregorian = new IntlDateFormatter(
        'de_DE@calendar=gregorian',
        IntlDateFormatter::MEDIUM, //date format
        IntlDateFormatter::SHORT, //time format
        'Europe/Berlin',
        IntlDateFormatter::TRADITIONAL
    );
    $timeStamp = $intlDateFormatterFromGregorian->parse('13 Sep 2022 13:46');
    $intlDateFormatterToForeignCalender = IntlDateFormatter::create("de_DE@calendar=" . $calendar,
        IntlDateFormatter::MEDIUM,
        IntlDateFormatter::SHORT,
        'Europe/Berlin',
        IntlDateFormatter::TRADITIONAL);

    $foreignDateDefaultFormat = $intlDateFormatterToForeignCalender->format($timeStamp);
    echo($foreignDateDefaultFormat . "\n");

    // Formatierung nach ICU-Regeln
    $intlDateFormatterToForeignCalender->setPattern('MM-dd-YYYY GG HH:mm:ss');
    $specialDateFormat = $intlDateFormatterToForeignCalender->format($timeStamp);
    echo($specialDateFormat . "\n");

    $intlDateFormatterFromForeignCalendar = new IntlDateFormatter(
        'de_DE@calendar=' . $calendar,
        IntlDateFormatter::MEDIUM,//date format
        IntlDateFormatter::SHORT, //time format
        'Europe/Berlin',
        IntlDateFormatter::TRADITIONAL
    );
    $timeStamp = $intlDateFormatterFromForeignCalendar->parse($foreignDateDefaultFormat);
    echo('tstamp: ' . $timeStamp . "\n");
    $intlaDateFormatterToGregorianCalendar = IntlDateFormatter::create("de_DE@calendar=gregorian",
        IntlDateFormatter::MEDIUM,
        IntlDateFormatter::SHORT,
        'Europe/Berlin',
        IntlDateFormatter::GREGORIAN
    );
    $defaultGregorianDateFormat = $intlaDateFormatterToGregorianCalendar->format($timeStamp);
    echo('This should be `09/13/22 13:46`' . "\n" . $defaultGregorianDateFormat . "\n\n");
}  
padina
  • 77
  • 5
  • Thanks for your attempt. The question about other calendars to Gregorian. Also, you may want to check out this https://github.com/salarmehr/cosmopolitan for a quick conversion from Gregorian to other calendars. – Handsome Nerd Feb 14 '23 at 05:54