Skip to content

Instantly share code, notes, and snippets.

@ZabdiBen
Created May 8, 2024 19:05
Show Gist options
  • Save ZabdiBen/3da21b36d94222383d2ba8bb3e7ab6bf to your computer and use it in GitHub Desktop.
Save ZabdiBen/3da21b36d94222383d2ba8bb3e7ab6bf to your computer and use it in GitHub Desktop.
get the Hebrew date from Gregorian date
class HebrewDate {
public static function hebdate($date) {
// Returns string of Hebrew Date by hebdate_lang
// Remove the sunset check as it's not applicable in a static context
$lang = get_option('hebdate_lang');
if ($lang == 'site'){
if (get_locale() == 'he_IL'){
$lang = 'hebrew';
}
else {
$lang = 'english';
}
}
$gregorian_date = mysql2date('m-d-Y', $date);
list($mon, $day, $year) = explode('-', $gregorian_date);
$juldate = gregoriantojd($mon, $day, $year);
$hebdate = jdtojewish($juldate, $lang == 'hebrew', CAL_JEWISH_ADD_GERESHAYIM + CAL_JEWISH_ADD_ALAFIM_GERESH);
if ($lang =='number'){
return $hebdate;
}
if ($lang == 'english'){
list($tmp, $enday, $enyear) = explode('/', $hebdate);
$enmon = jdmonthname($juldate, 4);
// replace Adar
if ($enmon == "AdarI" && self::hasLeapYear($juldate)){
$enmon = 'Adar A';
}
elseif ($enmon == "AdarI" && !self::hasLeapYear($juldate)){
$enmon = 'Adar';
}
elseif ($enmon == "AdarII") {
$enmon = 'Adar B';
}
return "$enday $enmon $enyear";
}
$hebdate = iconv("windows-1255", "UTF-8", $hebdate);
$hebdate = str_replace(array('אדר ב', 'אדר א'), array('אדרב', 'אדרא'), $hebdate);
list($heb_day, $heb_month, $heb_year) = explode(' ', $hebdate);
switch ($heb_month) {
case "אדר":
case "אדרא'":
case "'אדרא":
if (self::hasLeapYear($juldate)){
$heb_month='אדר א׳';
}
break;
case "אדרב":
case "אדרב'":
case "'אדרב":
if (self::hasLeapYear($juldate)){
$heb_month='אדר ב׳';
}
break;
case "חשון":
$heb_month = "מרחשוון";
break;
}
if (get_option('hebdate_hide_alafim') == 1){
$heb_year= str_replace("ה'", '', $heb_year);
}
$hebdate = "$heb_day ב$heb_month $heb_year";
// replace merchaot to hebrew
$hebdate = str_replace(['"', '\''], ['״', '׳'], $hebdate);
return apply_filters('hebdate', $hebdate);
}
private static function hasLeapYear($juldate) {
$hebdate = jdtojewish($juldate);
$hebyear = explode('/', $hebdate)[2];
return apply_filters('hebdate_hasLeapYear', jewishtojd(6, 1, $hebyear) != jewishtojd(7, 1, $hebyear));
}
}
$hebrew_date = HebrewDate::hebdate('05/08/2024');
echo '<pre>$hebrew_date<br />'; var_dump($hebrew_date); echo '</pre>'; // ל׳ בניסן ה׳תשפ״ד
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment