Created
September 2, 2016 19:34
-
-
Save SamAsEnd/70f2587c002070d2a1985f0741111554 to your computer and use it in GitHub Desktop.
Ethiopian Date conversion(based on JDN)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Return a true if the year is a leap year based on the ethiopian calendar. | |
* | |
* @param int $year the ethiopian date to be checked | |
* | |
* @return bool | |
*/ | |
function ethiopian_is_leap_year($year) | |
{ | |
if (!is_int($year)) { | |
trigger_error( | |
__FUNCTION__ . '() expects parameter 1 to be numeric. ' . | |
gettype($year) . ' given', | |
E_USER_WARNING | |
); | |
} | |
return ($year + 1) % 4 == 0; | |
} | |
/** | |
* Returns true if the <code>$month, $day and $year</code> passed | |
* are a valid date based on the ethiopian calendar. | |
* | |
* @param int $month Ethiopian month | |
* @param int $day Ethiopian day | |
* @param int $year Ethiopian year (negative for AD) | |
* | |
* @return bool | |
*/ | |
function ethiopian_checkdate($month, $day, $year) | |
{ | |
return | |
// validate all | |
is_int($month) && is_int($day) && is_int($year) && | |
// true if the day is btn 1 - 30 | |
($day <= 30 && $day >= 1) && | |
// true if the month is btn 1 - 13 | |
($month <= 13 && $month >= 1) && | |
// true if the month is 13 then day is btn 1 - 6 | |
($month == 13 ? $day <= 6 : true) && | |
// true if the month is 13 & day is 6 then year must be leap year | |
($month == 13 && $day == 6 ? | |
ethiopian_is_leap_year($year) : true); | |
} | |
/** | |
* Returns the julian date number representation of the | |
* given ethiopian date. | |
* | |
* @param int $month Ethiopian month | |
* @param int $day Ethiopian day | |
* @param int $year Ethiopian year (negative for AD) | |
* | |
* @return int | |
*/ | |
function ethiopiantojd($month, $day, $year) | |
{ | |
if (ethiopian_checkdate($month, $day, $year)) { | |
trigger_error( | |
'ethiopiantojd() expects the date to be valid.' . | |
' check ethiopian_checkdate() first', | |
E_USER_WARNING | |
); | |
} | |
return (1723856 + 365) + | |
365 * ($year - 1) + | |
(int)($year / 4) + | |
30 * $month + | |
$day - 31; | |
} | |
/** | |
* Returns the ethiopian date string which is represented by | |
* the passed jdn <br />. | |
* | |
* @param int $jdn the Julian Date Number | |
* | |
* @return string in a month/day/year format | |
*/ | |
function jdtoethiopian($jdn) | |
{ | |
if (!is_long($jdn)) { | |
trigger_error( | |
'jdToEthiopian() expects parameter 1 to be numeric. ' . | |
gettype($jdn) . ' given', | |
E_USER_WARNING | |
); | |
} | |
$r = (($jdn - 1723856) % 1461); | |
$n = ($r % 365) + 365 * (int)($r / 1460); | |
$year = 4 * (int)(($jdn - 1723856) / 1461) + | |
(int)($r / 365) - (int)($r / 1460); | |
$month = (int)($n / 30) + 1; | |
$day = ($n % 30) + 1; | |
return "$month/$day/$year"; | |
} |
Author
SamAsEnd
commented
Sep 2, 2016
- Julian Day Number
- PHP JDN implemetation
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment