datetimetwigsymfonyhijrisymfony-3.1

Symfony Date to Hijri date conversion in twig file


In symfony template we can use the formatters like

{{ news.created|date('Y-m-d') }}

I need something similar for the hijri date conversion. Meaning I provide the date in gregorian and it convert it to hijri in the twig template file may be something like

{{ news.created|hijridate }}

I searched a lot on forums etc but did not find something relevant specifically in twig template.


Solution

  • After not finding any solution I make my own twig extension. I am pasting it hopefully it will help any one the input is the date object like {{ newsitem.created|hdate }} and ouput is الأربعاء 10 رمضان 1437 هـ

    Write a twig extension in src/AppBundle/Twig/HdateExtension.php with the following code.

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

    }

    Then add the following in the services.yml file

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