Symfony日期到枝条文件中的Hijri日期转换

时间:2016-06-16 07:15:11

标签: datetime twig symfony hijri symfony-3.1

在symfony模板中,我们可以使用像

这样的格式化程序
{{ news.created|date('Y-m-d') }}

我需要类似的hijri日期转换。意思是我以gregorian提供日期,并将它转换为twig模板文件中的hijri可能类似

{{ news.created|hijridate }}

我在论坛等上搜索了很多,但没有在twig模板中找到特别相关的东西。

1 个答案:

答案 0 :(得分:3)

在找不到任何解决方案后,我自己制作了枝条。我正在粘贴它,希望它可以帮助任何人输入日期对象 {{ newsitem.created|hdate }}和输出是  الأربعاء10رمضان1437ه

使用以下代码在src/AppBundle/Twig/HdateExtension.php中编写一个枝条扩展名。

<?php
namespace AppBundle\Twig;

class HdateExtension extends \Twig_Extension
{

public function getFilters()
{
    return array(
      new \Twig_SimpleFilter('hdate', array($this, 'hdateConvert') )
    );
}


public function hdateConvert($date)
{

    if($date instanceof \DateTime){
        $dateDay = $date->format('N');
        $date->modify('+1 day');
        $year = $date->format('Y');
        $month = $date->format('m');
        $day = $date->format('d');
    }
    $dayH = array("الأثنين","الثلاثاء","الأربعاء","الخميس","الجمعة","السبت","الأحد");
    // actual calculation
    $newDate = $dayH[$dateDay-1]." ".HdateExtension::Greg2Hijri($day, $month, $year, true );
    return $newDate;
}
public function getName()
{
    return 'hdate_extension';
}

public function Greg2Hijri($day, $month, $year, $string = false)
{
    $day   = (int) $day;
    $month = (int) $month;
    $year  = (int) $year;

    if (($year > 1582) or (($year == 1582) and ($month > 10)) or (($year == 1582) and ($month == 10) and ($day > 14)))
    {
        $jd = HdateExtension::intPart((1461*($year+4800+HdateExtension::intPart(($month-14)/12)))/4)+HdateExtension::intPart((367*($month-2-12*(HdateExtension::intPart(($month-14)/12))))/12)-
            HdateExtension::intPart( (3* (HdateExtension::intPart(  ($year+4900+    HdateExtension::intPart( ($month-14)/12)     )/100)    )   ) /4)+$day-32075;
    }
    else
    {
        $jd = 367*$year-HdateExtension::intPart((7*($year+5001+HdateExtension::intPart(($month-9)/7)))/4)+HdateExtension::intPart((275*$month)/9)+$day+1729777;
    }

    $l = $jd-1948440+10632;
    $n = HdateExtension::intPart(($l-1)/10631);
    $l = $l-10631*$n+354;
    $j = (HdateExtension::intPart((10985-$l)/5316))*(HdateExtension::intPart((50*$l)/17719))+(HdateExtension::intPart($l/5670))*(HdateExtension::intPart((43*$l)/15238));
    $l = $l-(HdateExtension::intPart((30-$j)/15))*(HdateExtension::intPart((17719*$j)/50))-(HdateExtension::intPart($j/16))*(HdateExtension::intPart((15238*$j)/43))+29;

    $month = HdateExtension::intPart((24*$l)/709);
    $day   = $l-HdateExtension::intPart((709*$month)/24);
    $year  = 30*$n+$j-30;
    $mname = array("محرّم","صفر","ربيع الأوّل"," ربيع الثاني","جمادى الأولى","جمادى الثانية","رجب","شعبان","رمضان","شوّال","ذو القعدة","ذو الحجّة");
    $date = array();
    $date['year']  = $year;
    $date['month'] = $mname[$month-1];
    $month = $mname[$month-1];
    $date['day']   = $day;

    if (!$string)
        return $date;
    else
        return     "{$day}  {$month}  {$year}  هـ ";
}
public function intPart($float)
{
    if ($float < -0.0000001)
        return ceil($float - 0.0000001);
    else
        return floor($float + 0.0000001);
}

}

然后在services.yml文件中添加以下内容

  app.twig_extension:
  class: AppBundle\Twig\HdateExtension
  public: false
  tags:
      - { name: twig.extension }