如何从我的 IntlDateFormatter() 函数中获取特定结果。?
How to get specific result from my IntlDateFormatter() function.?
我有这个功能可以将公历日期转换为贾拉利(波斯)日期:
function toJalali ($date)
{
$fmt = new IntlDateFormatter("fa_IR@calendar=persian", IntlDateFormatter::FULL, IntlDateFormatter::FULL, 'Asia/Tehran', IntlDateFormatter::TRADITIONAL);
return $fmt->format($date);
}
当我调用它时 toJalali(time())
它给我完整的日期 (1397-8-17 ه.ش ...)
如何从上述函数中获取我的特定日期格式。?
我知道可以在构造函数中设置格式选项:
$fmt = new IntlDateFormatter("fa_IR@calendar=persian", IntlDateFormatter::FULL, IntlDateFormatter::FULL, 'Asia/Tehran', IntlDateFormatter::TRADITIONAL, 'MM/dd/yyyy');
但是当我想要其他时间格式时呢?
只喜欢 year : (yyyy)
或 (EEEE/yyyy/MM)
等等,所以我必须为每种日期格式编写单独的函数。?
谢谢。
要更改模式,您可以使用 setPattern 方法,如下所示:
$fmt = new IntlDateFormatter("fa_IR@calendar=persian", IntlDateFormatter::FULL, IntlDateFormatter::FULL, 'Asia/Tehran', IntlDateFormatter::TRADITIONAL, 'MM/dd/yyyy');
$fmt->setPattern('EEEE/yyyy/MM');
echo $fmt->format(0);
您的函数将如下所示:
function toJalali ($date, $pattern = null)
{
$fmt = new IntlDateFormatter("fa_IR@calendar=persian", IntlDateFormatter::FULL, IntlDateFormatter::FULL, 'Asia/Tehran', IntlDateFormatter::TRADITIONAL);
if ($pattern){
$fmt->setPattern($pattern);
}
return $fmt->format($date);
}
我有这个功能可以将公历日期转换为贾拉利(波斯)日期:
function toJalali ($date)
{
$fmt = new IntlDateFormatter("fa_IR@calendar=persian", IntlDateFormatter::FULL, IntlDateFormatter::FULL, 'Asia/Tehran', IntlDateFormatter::TRADITIONAL);
return $fmt->format($date);
}
当我调用它时 toJalali(time())
它给我完整的日期 (1397-8-17 ه.ش ...)
如何从上述函数中获取我的特定日期格式。?
我知道可以在构造函数中设置格式选项:
$fmt = new IntlDateFormatter("fa_IR@calendar=persian", IntlDateFormatter::FULL, IntlDateFormatter::FULL, 'Asia/Tehran', IntlDateFormatter::TRADITIONAL, 'MM/dd/yyyy');
但是当我想要其他时间格式时呢?
只喜欢 year : (yyyy)
或 (EEEE/yyyy/MM)
等等,所以我必须为每种日期格式编写单独的函数。?
谢谢。
要更改模式,您可以使用 setPattern 方法,如下所示:
$fmt = new IntlDateFormatter("fa_IR@calendar=persian", IntlDateFormatter::FULL, IntlDateFormatter::FULL, 'Asia/Tehran', IntlDateFormatter::TRADITIONAL, 'MM/dd/yyyy');
$fmt->setPattern('EEEE/yyyy/MM');
echo $fmt->format(0);
您的函数将如下所示:
function toJalali ($date, $pattern = null)
{
$fmt = new IntlDateFormatter("fa_IR@calendar=persian", IntlDateFormatter::FULL, IntlDateFormatter::FULL, 'Asia/Tehran', IntlDateFormatter::TRADITIONAL);
if ($pattern){
$fmt->setPattern($pattern);
}
return $fmt->format($date);
}