PHP intl可以将公历日期转换为其他日历类型

mso*_*soa 2 php intl hijri datetime-conversion

PHP intl可以将公历日期转换为其他日历类型.

例如Gregorian到Hijri,2017/04/11to1396/01/22

或者我们必须使用外部库来转换日期?

hon*_*hah 6

你可以使用这种方法:

function getDateTimeFromCalendarToFormat($format = 'yyyy-MM-dd HH:mm:ss',     $time = null, $locale = 'fa_IR', $calendar = 'persian', $timeZone = 'Asia/Tehran')
{
    if (empty($time)) {
        $time = new \DateTime();
    }

    $formatter = new \IntlDateFormatter("{$locale}@calendar={$calendar}", \IntlDateFormatter::FULL, \IntlDateFormatter::FULL, $timeZone, \IntlDateFormatter::TRADITIONAL);
    $dateArray = [];

    $formatter->setPattern($format);

    return $formatter->format($time);
}
Run Code Online (Sandbox Code Playgroud)

如果你打电话给getDateTimeFromCalendarToFormat('yyyy-MM-dd HH:mm:ss',null,'en_US')
Return
1396-06-27 13:50:21

如果你打电话给getDateTimeFromCalendarToFormat('yyyy-MM-dd HH:mm:ss',null,'fa_IR')
Return
????-??-?? ??:??:??

要使用的新模式字符串.格式化日期和时间记录了可能的模式