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");
}