Time Data Series: Getting Started with PHP Zmanim - AdatoSystems
Acknowledgments
In this blog, Iâm explaining concepts and techniques related to both the way so-called âJewish timesâ (zmanim) are calculated, as well as the techniques needed to use the PHP Zmanim library â a library of functions that let you easily calculate Jewish times.
The PHPZmanim library is maintained by Zachary Weixelbaum. As with so many things in tech today, PHP Zmanim is itself based on the foundational Kosher Java library by Eliyahu Hershfeld. I owe a huge debt of gratitude to both Zachary and Eliyahu â not only for the work they put into developing those libraries but also for the support they have given me as I came up to speed on both the technical and Jewish religious guidelines (halacha) needed to use them.
Introduction
âWhat time will afternoon prayers (Mincha) be this week?â is a deceptively complex question. Itâs deceptive because âafternoon prayersâ seems to be self-explanatory, but (as with so many things related to Jewish religious rules (halacha) thereâs a vast amount of background, commentary, and specificity required.
NOTE: If you are already comfortable with Jewish time concepts (zmanim), feel free to skip to the section labeled âInstalling PHP Zmanim.â I wonât be insulted nor hold it against you.
Itâs difficult to understand the importance of accurate time calculations on the Jewish religious day, week, and life. It figures into everything from when a fast starts and ends to when itâs too late (or too early) to say certain prayers to the moment when simply owning (let alone eating) bread is forbidden.
To put it another way, Iâll share a quote from Rabbi Abraham Joshua Heschel in his book âThe Sabbathâ,
The meaning of the Sabbath is to celebrate time rather than space. Six days a week we live under the tyranny of things of space; on the Sabbath we try to become attuned to holiness in time.
Rabbi Abraham Joshua Heschel, âThe Sabbathâ
Even with that explanation, some readers may still wonder, âWhy make such a big deal about it? Are you telling me God is going to hold it against you if youâre 5 minutes off?â
Itâs a valid question and worth a moment of discussion because otherwise, the need for, let alone the impact of, this blog (and the techniques and technologies it describes) is diminished.
No, of course, God doesnât care. Just like my track coach doesnât âcareâ if I warm up before running, do my off-season workouts, or drink enough water. Letâs face it: no matter how hard I train, my coach wonât be any healthier. However, my coach â who cares for me beyond my performance in the next race, wants ME to care. They want me to be fully committed, if only because they know my experience will be more fulfilling because of it.
In the same way, God doesnât âcare.â But we know (through the texts of the Torah and Talmud) that God wants the US to care. And so Jewish scholars through the millennia have attempted â using the evidence presented in the text â to eschew simple and simplistic answers and dig deeper to uncover the details.
Back to the question at hand: âWhat time are afternoon prayers?â To understand that, we have to first understand what defines âdayâ (versus ânightâ), how to understand what demarcation separates âmorningâ from âafternoon,â how to effectively divide the day into parts, and so much more.
While this blog will introduce some of these concepts and terms as needed, it should not be considered exhaustive by any stretch of the imagination.
It comes as no surprise that computers have made quick work of calculating these times, and now thereâs a wide array of websites and applications ready to do the job for us. Even for those who want to create their own programs to do these calculations, there are time-saving solutions in the form of code libraries like KosherJava and its many ports, including PHP, Swift, .Net, JavaScript, Python, and many more.
Nevertheless, itâs important to understand the theory behind the tools. Anyone who wants to create a website or app has to be certain their program isnât inadvertently giving the wrong times. While, as mentioned earlier, nobody will âgo to hellâ (so to speak) for praying at the wrong time, there is a strict commandment:
×Ö°×Ö´×¤Ö°× ÖľÖŁ× ×˘Ö´×ÖľÖźÖר ×ÖšÖĽ× ×ŞÖ´×ŞÖľÖźÖ× ×Ö´×ְ׊֚×Ö× ×Ö°×ָר־ּ×תָ ×ÖľÖź×Öą×Öš×ÖśÖ××Ö¸âlifnei iver lo titein michshol vâyareita mei-elohechaââDonât put a stumbling block before the blindâ
(Leviticus 19:14)
Itâs one thing if an individual gets something wrong by accident. Itâs quite another to provide bad information that causes someone (or worse, LOTS of folks) to make a mistake, believing the information they got (from your app) was accurate and trustworthy.
The Basic Times
With that in mind, weâll start with understanding the times that are fairly straightforward and the least affected by variations in Jewish tradition:
- Sunrise (Netz Hachma) is a good old nautical sunrise â the moment when the top edge of the disk of the sun touches the horizon.
- Sunset (Shkia) is the other side of the daily coin â the moment when the top edge of the sun drops just below the horizon.
- A âseasonal hourâ (shaâah) refers to the even division of daylight, the time between sunrise and sunset. There are always 12 seasonal hours, but they may be longer or shorter than a regular (on your clock) hour because the amount of daylight fluctuates with the season.
- Note that some traditions calculate a shaâah as 12 divisions of the time from dawn (alot hashachar, or just âalotâ when light is visible over the horizon, but before sunrise itself) to night (tzeis hakochavim, or just âtzeisâ, when NO light is visible over the horizon, after sunset). Weâll get more into both dawn (a lot) and night (tzeit) later in the blog.
- Seasonal minutes (shaâot zmaniyot) is 1/60 of a seasonal hour and, just like a seasonal hour, may be larger or smaller than a minute on your clock.
As you can see, sunrise and sunset form the fundamental core of all other time calculations, and a great number of important Jewish times during the day are derived directly from those. For example, in some traditions, youâll find:
- Shabbat begins on Friday night, 18 minutes before sunset (shkia)
- Shabbat ends on Saturday night 45 (or 50, or 72) minutes after sunset (shkia)
- âŚand so on.
Calculations where âItâs Complicatedâ
Once you have sunrise and sunset nailed down, the next step is to identify the two critical times of dawn and night. Dawn (Alot haShachar, or just âAlotâ) and night (Tzeis hakochavim, or just âTzeisâ) mirror each other the way sunrise and sunset do. Dawn is the moment when light (but not the sun) is visible over the horizon. Night is the moment when light is no longer visible.
Unfortunately, whether or not there is or isnât âlightâ is highly subjective, and therefore, the way itâs calculated ranges all over the map.
- Some traditions use a fixed number of clock minutes before sunrise or after sunset.
- Others will use astronomy to identify the time when the sun is below the horizon by a specific number of degrees.
- Others will use a formula, such as taking the amount of daylight (from sunrise to sunset), dividing that into 10 equal parts, and then subtracting that amount from sunrise or adding it to sunset.
- Still, others will take the time the sun is below the horizon on an âequalâ day (meaning the spring or fall equinox, when the amount of sunshine and darkness are exactly the same), calculate the difference (in clock minutes) between that time and sunrise, normalize those minutes against âseasonal minutesâ (âshaâot zmaniyotâ) for the specific day in question, and finally subtract that amount from sunrise (or add THAT to sunset) for the day in question.
Sound confusing? As the title of this section says, âItâs complicated.â
My point in describing these isnât to settle on a single âcorrectâ calculation but rather to help you understand the ultimate goal of each type of calculation â as well as for you to understand that there are multiple ways different Jewish traditions arrive at each calculation.
The Complex times
Once you have dawn (alot), sunrise (netz), sunset (shkia), and night (tzeis), it becomes possible to calculate other important daily times. These include:
- The earliest time for afternoon prayers (Mincha Gedola), which is often calculated as a certain number of seasonal hours (shaâah) after either dawn or sunrise. The point of this time is that it is the earliest moment when âafternoonâ (literally, after mid-day) happens.
- The preferable time for afternoon prayers (Mincha ketana), which is calculated as a larger number of seasonal hours (shaâah) after either sunrise (netz) or dawn (alot). The point here is that itâs more than 3/4 of the way through the day, when itâs CLEARLY afternoon â but without the risk of it accidentally being night (tzeis).
- The latest time for afternoon prayers (Plag haMincha, or âplagâ) , which is often calculated as the time between the best time for afternoon prayers (mincha ketana) and night (tzeis).
While there are several other key times that weâll get to in a different blog, these are enough to get us started.
Installing the PHP Zmanim Library
With those technical details taken care of, we can now move on to⌠some technical details! Except this time, they are TECHNICAL technical details. (and for those people who skipped all the way from the introduction to this section, WELCOME! Itâs great to have you back!)
PHP Zmanim is a complex library that comprises not just the import of Kosher Java into the PHP language but also some supporting modules like Carbon (a tool that helps make date and time conversions faster, easier, and more accurate); Symfony (a web design framework); and a few other smaller utilities.
The point is that all those modules work in concert with PHP Zmanim, and you need all of them installed for anything to work. The easiest way to get them all installed is using the PHP âcomposerâ utility that manages dependencies.
If you need to run PHP Zmanim on a web server and donât have access to run installers at the command line, youâre still in luck. Simply copy the ./vendor directory from a local installation, and â with some caveats â it will work.
What are those caveats? The main one is to make sure the version of PHP supported by your hosting provider matches the one you use when installing via Composer.
This brings me to the actual composer install command. I know we havenât gotten to actually coding anything yet, but letâs set up a directory where the scripts are going to go. For this blog, Iâm going to call it /phpzmanim_test.
In a terminal window, move to that directory:
cd /php zmanim_test
Now type the command:
composer -V
(note thatâs a capital V)
This will confirm whether composer is installed or not. If not, do a quick search to find out the best way to get it installed on your operating system.
Next, type the command:composer require zachweix/php-zmanim
This will do two things:
- First, it will create the fileÂ
composer.json
 in the current directory, with instructions that PHP Zmanim should be installed. - It actually installs PHP Zmanim for you, into a ./vendor directory inside the current directory. No extra steps are needed.
The only other âcomposerâ related item to note is that if you ever need to update, you can do so easily by going into the /phpzmanim_test directory (or whatever you called yours) and typing the command composer update
. This will read the information in the composer.json file and update everything it finds there.
(SPOILER: this means you should NOT delete the composer.json file. You donât, however, have to copy it to your remote site with the rest of your application if you donât want to.)
Once this is done, you need to include the following line in every PHP script that will use the PHP Zmanim library:
require 'vendor/autoload.php';
Getting Started with PHP Zmanim
Now that PHP Zmanim is installed, youâre ready to start writing some code with it.
To do this, youâll first create a php object â a collection (array, really) of data that are passed into various functions. This object contains both the specific date for the times youâll want to pull and also the precise location information.
Creating an object with âZmanim::create()â
Before you start, youâll need to know a few things about the location where you are asking for times:
- The latitude and longitude.
- The time zone is formatted using the tz database format. (example: âAmerica/New_Yorkâ).
- The elevation of that location, if you choose to use it. (SPOILER: very rarely does elevation factor into these calculations.)
- The year, month, and day of the times youâll be calculating.
The format for creating your object is:
$VARNAME = Zmanim::create(YEAR, MONTH, DAY, "NAME OF LOCATION", LATITUDE, LONGITUDE, ELEVATION, "TIME ZONE");
Hereâs a specific example:
$zmanim = Zmanim::create(2019, 2, 21, "New York City", 40.850519, -73.929214, 200, "America/New_York");
What that looks like as a script with some variables in place of the literal input:
<?php
require 'vendor/autoload.php';
use PhpZmanim\Zmanim;
use PhpZmanim\Calendar\ComplexZmanimCalendar;
use PhpZmanim\Geo\GeoLocation;
$getdate = date('Y-m-d');
$getyear = date('Y', strtotime($getdate));
$getmonth = date('m', strtotime($getdate));
$getday = date('d', strtotime($getdate));
$locname = "Beit Knesset Chochmat Shlomo, Beachwood, OH";
$lat = 41.4939407;
$long = -81.516709;
$elev = 0;
$tz = 'America/New_York';
$zmanim = Zmanim::create($getyear, $getmonth, $getday, $locname, $lat, $long, $elev, $tz);
With that code in place, you have an object for todayâs date, for the specified location. You can even print it out using PHPâs print_r()
 function:
print_r($zmanim);
Which should give you output that looks something like this:
PhpZmanim\Zmanim Object
(
[calendar:PhpZmanim\Calendar\AstronomicalCalendar:private] => Carbon\Carbon Object
(
[endOfTime:protected] =>
[startOfTime:protected] =>
[constructedObjectId:protected] => 00000000000000080000000000000000
[localMonthsOverflow:protected] =>
[localYearsOverflow:protected] =>
[localStrictModeEnabled:protected] =>
[localHumanDiffOptions:protected] =>
[localToStringFormat:protected] =>
[localSerializer:protected] =>
[localMacros:protected] =>
[localGenericMacros:protected] =>
[localFormatFunction:protected] =>
[localTranslator:protected] =>
[dumpProperties:protected] => Array
(
[0] => date
[1] => timezone_type
[2] => timezone
)
(thereâs more, but you get the idea).
Getting your first real-time â Sunrise (Netz HaHachma)
Once you have an object created, using it to get specific times through PHP Zmanim is almost anti-climactic. Getting sunrise is as simple as:
$sunrise = $zmanim->sunrise;
If you arenât familiar with it or havenât used it before, the ->
 indicates youâre querying an object (again, a collection of data elements) or using a built-in method.
This will return something that looks like this:
2024-12-20 07:48:52
For those following along, the complete code so far looks like this:
<?php
require 'vendor/autoload.php';
use PhpZmanim\Zmanim;
use PhpZmanim\Calendar\ComplexZmanimCalendar;
use PhpZmanim\Geo\GeoLocation;
# Set variables:
#41.4939407, -81.516709;
$locname = "Beit Knesset Chochmat Shlomo, Beachwood, OH";
$lat = 41.4939407;
$long = -81.516709;
$elev = 0;
$tz = 'America/New_York';
$getyear = 2024;
$getday = 20;
$getmonth = 12;
$testzmanim = Zmanim::create($getyear, $getmonth, $getday, $locname, $lat, $long, $elev, $tz);
$sunrise = $testzmanim->sunrise;
echo "$sunrise\n";
?>
Summary
There is a lot more to cover in PHP Zmanim, which I plan to do in the coming weeks. But this should at least get you started with building a PHP-based script or website and displaying accurate times for Jewish moments.
As always, I welcome questions, corrections, and kudos in the comments below.
Also published here.