How to convert Gregorian dates to Jewish dates?

html code
Photo by Pixabay on Pexels.com

It has been a long while since when I have last written any example scripts on here but one I have found on Ask a Jew when checking search results is how to convert dates to a Jewish calendar using the PHP language.

Below is a code I find when Googling this and I am hoping you notice something wrong with it because it is a major issue.

<?php
function convertToHebrew($englishDate) {
  $englishDateParts = explode("/", $englishDate);
  $month = $englishDateParts[0];
  $day = $englishDateParts[1];
  $year = $englishDateParts[2];
  $hebrewDate = "";
  $year = convertYearToHebrew($year);
  $hebrewMonths = array("Tishrei", "Cheshvan", "Kislev", "Tevet", "Shevat", "Adar", "Nissan", "Iyar", "Sivan", "Tammuz", "Av", "Elul");
  $hebrewMonth = $hebrewMonths[$month - 1];
  $hebrewDate .= $day . " " . $hebrewMonth . " " . $year;
  
  return $hebrewDate;
}

function convertYearToHebrew($year) {
  $hebrewChars = array("א", "ב", "ג", "ד", "ה", "ו", "ז", "ח", "ט", "י");  
  $yearInHebrew = "";
  while ($year > 0) {
    $yearInHebrew = $hebrewChars[$year % 10] . $yearInHebrew;
    $year = (int)($year / 10);
  }
  return $yearInHebrew;
}
echo convertToHebrew("10/3/2022");
?>

I am hoping you noticed that the above doesn’t actually do what we wanted – all it does is change the name of the month! If you know anything about the Jewish calendar it has a different year and works based on the lunar calendar – so the 25 of Kislev isn’t always on the same day each year when in the calendar we all use every day. The reason for the drift as the Jewish calendar is based on the moon and becomes a new month when the moon changes and it was never exactly defined when it should be in ancient times.

With this in mind, I am going to show you how to use a PHP library called “PHP-Calender” to do this for you.

The PHP-Calendar library provides a set of classes for working with different calendar systems in PHP. The JewishCalendar class in particular allows you to convert dates to the Jewish calendar.

The JewishCalendar class extends the Calendar class, which provides the basic functionality for working with dates in PHP. The JewishCalendar class adds additional methods for working with dates in the Jewish calendar, such as getJewishYear(), which returns the year in the Jewish calendar for a given date, and getJewishMonth(), which returns the month in the Jewish calendar for a given date.

You can download the PHP-Calendar library from its GitHub page at the following link: https://github.com/amnuts/php-calendar although when I had a quick look it seems to be down as of posting – I will check back later and update it should the link be gone.

Below is an example of how you can get the Jewish year from any date but naturally you can use it to get the day, month etc. I use the same code below on this very website to get the Jewish year which you can find next to each post.

<?php

// Include the library in your PHP script
require_once('/path/to/PHP-Calendar/src/Calendar/JewishCalendar.php');

// Create a new instance of the JewishCalendar class
$cal = new \Calendar\JewishCalendar();

// Set the date you want to convert
$cal->setDate(2022, 12, 8); // December 8, 2022

// Get the year in the Jewish calendar
$year = $cal->getJewishYear();

// Print the year
echo "The year in the Jewish calendar is: $year";

?>

I hope the following helps anyone that might find this on Google – I know my other website keeps getting this appearing in search results somehow.

Need to reference?

Ellis, M. (2022). How to convert Gregorian dates to Jewish dates?. [online] Snat's Narratives & Tales. Available at: https://snat.co.uk/stem/scripts/how-to-convert-gregorian-dates-to-jewish-dates.html [Accessed 27 Apr 2024].

Thanks for reading! You may be interested in this …

Join the Discussion

Learn how to convert dates to the Jewish calendar using PHP language correctly, and why the code example provided in this article won't work.

Add to the discussion!

This site uses Akismet to reduce spam. Learn how your comment data is processed.