【问题标题】:Symfony Date to Hijri date conversion in twig file树枝文件中的 Symfony 日期到回历日期转换
【发布时间】:2016-06-16 07:15:11
【问题描述】:

在 symfony 模板中,我们可以使用格式器,例如

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

我需要类似的东西来进行回历日期转换。这意味着我在 gregorian 中提供日期,并将其转换为 twig 模板文件中的回历可能类似于

{{ news.created|hijridate }}

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

【问题讨论】:

    标签: datetime twig symfony hijri symfony-3.1


    【解决方案1】:

    在没有找到任何解决方案后,我制作了自己的树枝扩展。我正在粘贴它,希望它对输入日期对象的任何人都有帮助 {{ 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 }
    

    【讨论】:

      【解决方案2】:

      感谢您的代码, symfony 5:

      <?php
      
      namespace App\Twig;
      
      use Exception;
      use Twig\Extension\AbstractExtension;
      use Twig\TwigFilter;
      
      class HdateExtension extends AbstractExtension {
          /**
           * @return TwigFilter[]
           */
          public function getFilters(): array {
              return [
                  new TwigFilter(
                      'hdate', [
                      $this, 'hdateConvert'
                  ])
              ];
          }
      
          /**
           * @param $arg
           * @return string
           * @throws Exception
           */
          public function hdateConvert($arg): string {
              if ($arg instanceof \DateTime) {
                  $dayNumber = $arg->format('N');
                  $arg->modify('+1 day');
                  $year = $arg->format('Y');
                  $month = $arg->format('n');
                  $day = $arg->format('j');
      
                  $dayH = [
                      "الأثنين",
                      "الثلاثاء",
                      "الأربعاء",
                      "الخميس",
                      "الجمعة",
                      "السبت",
                      "الأحد"
                  ];
      
                  return $dayH[$dayNumber - 1] . " " . HdateExtension::Greg2Hijri($day, $month, $year);
              }
      
              throw new Exception('Invalid date');
          }
      
          /**
           * @return string
           */
          public function getName(): string {
              return 'hdate_extension';
          }
      
          /**
           * @param int $day
           * @param int $month
           * @param int $year
           * @return string
           */
          public function Greg2Hijri(int $day, int $month, int $year): string {
              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 = [
                  "محرّم",
                  "صفر",
                  "ربيع الأوّل",
                  " ربيع الثاني",
                  "جمادى الأولى",
                  "جمادى الثانية",
                  "رجب",
                  "شعبان",
                  "رمضان",
                  "شوّال",
                  "ذو القعدة",
                  "ذو الحجّة"
              ];
      
              return "{$day}  {$mname[$month - 1]}  {$year}  هـ ";
          }
      
          /**
           * @param $float
           * @return false|float
           */
          private function intPart($float) {
              if ($float < -0.0000001)
                  return ceil($float - 0.0000001);
              else
                  return floor($float + 0.0000001);
          }
      }
      

      【讨论】:

      • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-03-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多