Skip to content
Advertisement

Get today coptic date

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:

Advertisement

Answer

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.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement