2

我希望使用任何代码类型获取今天的科普特日期,php或者javascript在我的网站标题中显示科普特日期。我需要阿拉伯语或英语。

我试图找到它,但在英语中没有找到任何类似的东西。

参考:

4

2 回答 2

2

这是DateTime扩展。

使用示例:

$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

由于这是 DateTime 类的扩展,您可以进行各种日期时间修改:

$dt->modify('-2 year');
echo $dt->coptic(); # Tout 19, 1728

$dt->add(new DateInterval('P7Y5M4D'));
echo $dt->coptic(); # Amshir 26, 1735

# etc.

代码:

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

}

由于闰年,此代码不适用于 1900 年以下的日期。

于 2013-09-29T16:38:09.673 回答
0

要获得今天的英语和阿拉伯语科普特日期的简单方法是Intl.DateTimeFormat()使用以下方法(您可以自定义输出格式):

console.log(new Intl.DateTimeFormat('en', { calendar:'coptic', dateStyle:"full"}).format(new Date()));

console.log(new Intl.DateTimeFormat('ar-u-nu-arab', { calendar:'coptic', dateStyle:"full"}).format(new Date()));

console.log(new Intl.DateTimeFormat('en', { calendar:'coptic', dateStyle:"full"}).format(new Date()));

console.log(new Intl.DateTimeFormat('ar-u-nu-arab', { calendar:'coptic', dateStyle:"full"}).format(new Date()));

于 2022-02-25T14:02:27.240 回答