PHP intl可以将公历日期转换为其他日历类型 [英] PHP intl can convert a Gregorian date to other calendar type

查看:122
本文介绍了PHP intl可以将公历日期转换为其他日历类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

PHP intl can convert a Gregorian date to other calendar type.

例如,将公历日期转换为回历, 2017/04/11 1396/01/22

For example Gregorian to Hijri, 2017/04/11 to 1396/01/22

还是必须使用外部库转换日期?

Or we must use external library to convert dates?

推荐答案

您可以使用以下方法:

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

如果您调用 getDateTimeFromCalendarToFormat('yyyy-MM- dd HH:mm:ss',null,'en_US')

返回

1396 -06-27 13:50:21

如果您调用 getDateTimeFromCalendarToFormat('yyyy-MM-dd HH: mm:ss',null,'fa_IR')

返回

۱۳۹۶-۰۶- ۲۷ ۱۳:۴۹:۵۱

要使用的新模式字符串。在格式化日期和时间

New pattern string to use. Possible patterns are documented at Formatting Dates and Times

这篇关于PHP intl可以将公历日期转换为其他日历类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆