我希望获取今天的科幻日期,使用任何代码类型,例如php
或javascript
,以便在我的网站标题中显示Coptic日期。我需要阿拉伯语或英语。
我试图找到它,但没有用英语发现任何类似的东西。
参考:
答案 0 :(得分:1)
$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年以后的日期无效。