PHP intl 可以将公历日期转换为其他日历类型。
例如公历到回历,2017/04/11
到1396/01/22
或者我们必须使用外部库来转换日期?
PHP intl 可以将公历日期转换为其他日历类型。
例如公历到回历,2017/04/11
到1396/01/22
或者我们必须使用外部库来转换日期?
您可以使用此方法:
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')
Return
1396-06-27 13:50:21
如果您致电getDateTimeFromCalendarToFormat('yyyy-MM-dd HH:mm:ss',null,'fa_IR')
Return
۱۳۹۶-۰۶-۲۷ ۱۳:۴۹:۵۱
要使用的新模式字符串。格式化日期和时间记录了可能的模式
是的,它可以。这是一个使用 intl 将时间对象转换为其波斯格式字符串的示例:
function c2persian($time, $toCalender = 'persian', $timezone = 'Asia/Tehran', $locale = 'fa_IR') {
$formatter = IntlDateFormatter::create($locale, NULL, NULL, $timezone, IntlCalendar::createInstance($timezone, "$locale@calendar=$toCalender"));
return $formatter->format($time);
}
$time = strtotime("2089-08-09 00:00:00 UTC");
echo c2persian($time);
您可以在php intlCalendar 文档中找到更多信息。
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 日期的更多信息:http: //php.net/manual/en/function.date.php