PHP intl可以将公历日期转换为其他日历类型
问题描述:
答
PHP已经有日期格式的功能,你需要用strtotime()
功能首先转换并与date()
<?php
$originalDate = "2017/04/11";
$theDate = strtotime($originalDate);
$theDay = date('d',$theDate);
$theMonth = date('m',$theDate);
$theYear = date('Y',$theDate);
$customFormat = date('Y-m-d',$theDate);
$customFormat2 = date('d/m/Y',$theDate);
$customFormat3 = date('F j, Y, g:i a',$theDate);
?>
例子在这里工作得到所需的值:https://eval.in/772416
您可以获取有关日期的详细信息php here:http://php.net/manual/en/function.date.php
答
您可以使用此方法:
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')
返回۱۳۹۶-۰۶-۲۷ ۱۳:۴۹:۵۱
http://php.net/manual/en/ref.calendar .PHP – aynber