douhao2011 2013-09-28 17:21
浏览 29
已采纳

今天获得科普特日期

I'm looking to get coptic date of today with any code type like php or javascript to display coptic date to my site header. I need it in arabic or english.

I have tried to find it, but didn't found any thing like it in english.

Reference:

  • 写回答

1条回答 默认 最新

  • dousao1175 2013-09-29 16:38
    关注

    Here is the extension of the DateTime class.

    Use example:

    $dt = new CopticDateTime;
    
    echo $dt->coptic(); # Tout 19, 1730
    echo $dt->coptic('F j, Y'); # same as above (default)
    
    echo $dt->coptic('d.m.Y'); # 19.01.1730
    
    echo $dt->coptic('Y/n/j'); # 1730/1/19
    

    Since this is the extend of DateTime class, you can do all kind of date-time modifications :

    $dt->modify('-2 year');
    echo $dt->coptic(); # Tout 19, 1728
    
    $dt->add(new DateInterval('P7Y5M4D'));
    echo $dt->coptic(); # Amshir 26, 1735
    
    # etc.
    

    Code:

    class CopticDateTime extends DateTime {
    
        private $coptic_months = [
            [ 1, 'Tout',      '09-11', '09-12'],
            [ 2, 'Baba',      '10-11', '10-12'],
            [ 3, 'Hator',     '11-10', '11-11'],
            [ 4, 'Kiahk',     '12-10', '12-11'],
            [ 5, 'Toba',      '01-09', '01-10'],
            [ 6, 'Amshir',    '02-08', '02-09'],
            [ 7, 'Baramhat',  '03-10', '03-10'],
            [ 8, 'Baramouda', '04-09', '04-09'],
            [ 9, 'Bashans',   '05-09', '05-09'],
            [10, 'Paona',     '06-08', '06-08'],
            [11, 'Epep',      '07-08', '07-08'],
            [12, 'Mesra',     '08-07', '08-07'],
            [13, 'Nasie',     '09-06', '09-06'],
        ];
    
        public function coptic($format = 'F j, Y')
        {
            $year = $this->getCopticYear();
            $month = $this->getCopticMonth();
            $day = $this->getCopticDay($month);
    
            $replace = [
                'Y' => $year,
                'F' => $month[1],
                'n' => $month[0],
                'm' => sprintf('%02d', $month[0]),
                'j' => $day,
                'd' => sprintf('%02d', $day),
            ];
    
            $replaceKeys = array_map(function($r) { return '{' . $r .'}'; }, array_keys($replace));
            $format = str_replace(array_keys($replace), $replaceKeys, $format);
            return str_replace($replaceKeys, $replace, $format);
        }
    
        private function getCopticYear()
        {
            $dateColumn = $this->format('L') ? 3 : 2;
            $date = $this->coptic_months[0][$dateColumn];
            return $this->format('Y') - 283 - ($this->format('m-d') < $date ? 1 : 0);
        }
    
        private function getCopticMonth()
        {
            $dateColumn = $this->format('L') ? 3 : 2;
            $date = $this->format('m-d');
            $month = null;
            foreach ($this->coptic_months as $copticMonth) {
                if ($date >= $copticMonth[$dateColumn]) {
                    $month = $copticMonth;
                    if ($copticMonth[$dateColumn] >= '12-00') break;
                } elseif ($month) {
                    break;
                }
            }
            if (!$month) {
                $month = $this->coptic_months[3];
            }
            return $month;
        }
    
        private function getCopticDay(array $month)
        {
            $dateColumn = $this->format('L') ? 3 : 2;
            $monthDateTime = clone $this;
            list($m, $d) = explode('-', $month[$dateColumn]);
            $monthDateTime->setDate($this->format('Y'), $m, $d);
            if ($monthDateTime > $this) $monthDateTime->modify('-1 year');
            return $monthDateTime->diff($this)->days + 1;
        }
    
    }
    

    This code doesn't work correctly for dates bellow year 1900, because of the leap years.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog