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:
- http://www.copticchurch.net/easter.html
- http://www.ortelius.de/kalender/coptic_en.php
- http://en.wikipedia.org/wiki/Coptic_calendar
Advertisement
Answer
Here is the extension of the DateTime class.
Use example:
JavaScript
x
9
1
$dt = new CopticDateTime;
2
3
echo $dt->coptic(); # Tout 19, 1730
4
echo $dt->coptic('F j, Y'); # same as above (default)
5
6
echo $dt->coptic('d.m.Y'); # 19.01.1730
7
8
echo $dt->coptic('Y/n/j'); # 1730/1/19
9
Since this is the extend of DateTime class, you can do all kind of date-time modifications :
JavaScript
1
8
1
$dt->modify('-2 year');
2
echo $dt->coptic(); # Tout 19, 1728
3
4
$dt->add(new DateInterval('P7Y5M4D'));
5
echo $dt->coptic(); # Amshir 26, 1735
6
7
# etc.
8
Code:
JavaScript
1
76
76
1
class CopticDateTime extends DateTime {
2
3
private $coptic_months = [
4
[ 1, 'Tout', '09-11', '09-12'],
5
[ 2, 'Baba', '10-11', '10-12'],
6
[ 3, 'Hator', '11-10', '11-11'],
7
[ 4, 'Kiahk', '12-10', '12-11'],
8
[ 5, 'Toba', '01-09', '01-10'],
9
[ 6, 'Amshir', '02-08', '02-09'],
10
[ 7, 'Baramhat', '03-10', '03-10'],
11
[ 8, 'Baramouda', '04-09', '04-09'],
12
[ 9, 'Bashans', '05-09', '05-09'],
13
[10, 'Paona', '06-08', '06-08'],
14
[11, 'Epep', '07-08', '07-08'],
15
[12, 'Mesra', '08-07', '08-07'],
16
[13, 'Nasie', '09-06', '09-06'],
17
];
18
19
public function coptic($format = 'F j, Y')
20
{
21
$year = $this->getCopticYear();
22
$month = $this->getCopticMonth();
23
$day = $this->getCopticDay($month);
24
25
$replace = [
26
'Y' => $year,
27
'F' => $month[1],
28
'n' => $month[0],
29
'm' => sprintf('%02d', $month[0]),
30
'j' => $day,
31
'd' => sprintf('%02d', $day),
32
];
33
34
$replaceKeys = array_map(function($r) { return '{' . $r .'}'; }, array_keys($replace));
35
$format = str_replace(array_keys($replace), $replaceKeys, $format);
36
return str_replace($replaceKeys, $replace, $format);
37
}
38
39
private function getCopticYear()
40
{
41
$dateColumn = $this->format('L') ? 3 : 2;
42
$date = $this->coptic_months[0][$dateColumn];
43
return $this->format('Y') - 283 - ($this->format('m-d') < $date ? 1 : 0);
44
}
45
46
private function getCopticMonth()
47
{
48
$dateColumn = $this->format('L') ? 3 : 2;
49
$date = $this->format('m-d');
50
$month = null;
51
foreach ($this->coptic_months as $copticMonth) {
52
if ($date >= $copticMonth[$dateColumn]) {
53
$month = $copticMonth;
54
if ($copticMonth[$dateColumn] >= '12-00') break;
55
} elseif ($month) {
56
break;
57
}
58
}
59
if (!$month) {
60
$month = $this->coptic_months[3];
61
}
62
return $month;
63
}
64
65
private function getCopticDay(array $month)
66
{
67
$dateColumn = $this->format('L') ? 3 : 2;
68
$monthDateTime = clone $this;
69
list($m, $d) = explode('-', $month[$dateColumn]);
70
$monthDateTime->setDate($this->format('Y'), $m, $d);
71
if ($monthDateTime > $this) $monthDateTime->modify('-1 year');
72
return $monthDateTime->diff($this)->days + 1;
73
}
74
75
}
76
This code doesn’t work correctly for dates bellow year 1900, because of the leap years.