」工欲善其事,必先利其器。「—孔子《論語.錄靈公》
首頁 > 程式設計 > 時間數據系列:故事的其餘部分

時間數據系列:故事的其餘部分

發佈於2024-11-09
瀏覽:317

Time Data Series: The Rest of the Story

Time Data Series: The Rest of the Story - AdatoSystems

It’s been a while since I wrote about PHP Zmanim – the work I’ve done with it and the things I’ve learned while implementing it. But despite the delay, I always intended to continue the conversation. That’s what we’re doing today 

In my first post I explained how to install and get started with the PHP Zmanim library. Then in the next post I dug into calculating more complex times, and the real power of the tool – applying the most common Rabbinic opinions for various zmanim. I’m picking up where I left off with minimal overlap, so if you need to take a minute to get back up to speed,  I’ve linked to those previous posts.

The goal today is to explore uses of PHP Zmanim that move beyond the relatively straightforward use of the library. That includes:

  • Taking a standard time and adjusting it for synagogue specific use cases (“Mincha starts 25 minutes before Shkia each day”).
  • Using PHP Zmanim library functions and methods that provide non-time calculations – such as the weekly Torah Portion, the Hebrew date, and whether a specified date is a holiday (like Rosh Chodesh or Sukkot).

Acknowledgements

Once again need to begin by stating my gratitude to the folks who made all of this possible. Foremost among a long list of names is Zachary Weixelbaum, the maintainer of the PHP Zmanim library itself; and Eliyahu Hershfeld: creator of the Kosher Java library upon which PHP Zmanim is based.

A quick refresher

When we left off, we had a solid basic php script that:

  • sets variables for the location (in latitude/longitude), along with the time zone and elevation.
  • Sets the date
  • creates a php zmanim object from those variables
  • uses the built-in methods to calculate the time for anything from sunrise to mincha to tzeit hakochavim

It looked like this:

sunrise;
echo "$sunrise\n";
?>

But sunrise is just the beginning. Using any of the built-in calculations (which are described on the github readme page), you can pull a wide variety of times. For example, to get Mincha Gedola using a specific Rabbinic opinion, I could include:

$gedolah = $zmanim->minchaGedola16Point1Degrees

In the last blog I also covered ways to use PHP Zmanim’s format method to make the output more readable:

$gedolah = $gedolah->format('g:i a');

“Salt to taste” – adjusting times

For those who don’t spend a lot of time in synagogue (no blame, no shame, you’re still welcome here) it may not be obvious, but – despite the name of the zman – very few organizations pray Mincha (afternoon prayers) at Mincha Ketana, Mincha Gedola, or Plag haMincha.

In fact, a lot of the work with regard to calculating times has less to do with the straight “what time is Mincha Gedola?” and more with “how late in the day can we reasonably schedule Mincha so that everyone has time to get here after work; but not SO late that nobody comes because they’ll miss dinner?”

If that’s too theoretical, allow me to share the logical jenga puzzle my synagogue considers when setting up the weekly schedule:

  • Friday night candle lighting: Shkia (sunset) minus 18 minutes
  • Summer Friday Mincha: Plag haMincha (using “plagHaminchaAteretTorah”) minus 20 minutes
  • Winter Friday Mincha: Shkia (sunset) minus 22 minutes
  • Saturday Mincha: Shkia minus 40 minutes
  • Saturday Ma’ariv: Shkia plus 50 minutes
  • Saturday end of Shabbat: Shkia plus 45 minutes
  • Weekday Mincha: find the earliest shkia for the week, then subtract 17 minutes

While all the other times I covered in earlier blogs still matter, hopefully you are starting to realize that they matter less often than one might expect.

So how DO you take a time (like shkia/sunset) and then add or subtract minutes? We’ll start off with the same script we had before:

To that we’ll add the code to get sunset:

$sunset = $zmanim->sunset;

and then we’ll subtract 18 minutes to get candle lighting time:

$candles = date('g:i a', strtotime($sunset . " -18 minutes"));

That’s right. You just use php’s own string to time function. I’m sorry if I kept you in suspense. But it’s really just that easy.

What’s the Torah portion? What’s the Hebrew date? When is Rosh Chodesh?

(…and other essential but vexing questions often asked in and around your synagogue.)

Along with date calculations, the KosherJava library (and therefore the PHP Zmanim library) have methods and functions to quickly provide information like the ones asked above.

The first thing to understand is that the PHP Zmanim object we’ve been working with so far has been a Zmanim object – an object that has a set of specific times for a particular date. For things involving the dates themselves, we instantiate a  jewishCalendar object. The good news is that it’s MUCH easier to create. All you need is the year, month, and day.

We now have a jewishCalendar object to work with and can use methods similar to the ones we found for times. For example: Presuming the date we selected was a Saturday, we can get the Torah portion:

$format = Zmanim::format();
$parshaeng = json_decode('"' . $format->formatParsha($jewishCalendar) . '"');

(ignore the $format setting for now. We’ll dig into it in just a bit).

If you ran that code, $parshaeng would give you:

Parshas Ki Savo

You’ll notice we didn’t need to provide latitude, longitude, time zone, etc. I cannot stress enough that this capability alone – the ability to get dates, Torah portions, and more – makes the PHP Zmanim library useful all on its own, even without the time calculations. 

Can I get that in Hebrew?

The Torah Portion? Of course! This is where the $format line comes in. 

First, an explanation of what it is: It’s a method that modifies objects like Zmanim and jewishCalendar, setting the display and output options.  Setting Zmanim::format() without any other information defaults to English. But then you can tell the system you want Hebrew with this additional line:

$format->setHebrewFormat(true)

Now if you run the same json_decode(‘”‘ . $format->formatParsha($jewishCalendar) . ‘”‘) line, you’d get:

כי תבוא

Putting it all together:

formatParsha($jewishCalendar) . '"');
$format->setHebrewFormat(true);
$parshaheb = json_decode('"' . $format->formatParsha($jewishCalendar) . '"');
echo "$parshaheb - $parshaeng\n";

This would display: כי תבוא – Ki Savo

What day is it?

More specifically, what HEBREW day is it?

Like the previous example, we start out with a jewishCalendar and a format object

$jewishCalendar = Zmanim::jewishCalendar(Carbon::createFromDate($theyear, $themonth, $theday));
$format = Zmanim::format();

Then we add the same line to set the format to Hebrew.

$format->setHebrewFormat(true);

And finally (and fairly simply) we ask for a straight calendar object output:

$zmandate = json_decode('"' . $format->format($jewishCalendar) . '"');

That’s literally all you need. The result is something like this:

17 Elul, 5784

Yes, even though we specified setHebrewFormat it came out in English.

Can I get that in ACTUAL Hebrew?

In order to get the Hebrew date to show up fully in Hebrew – both words and letters – we invoke a slightly different method called, appropriately enough, HebrewDateFormatter. Presuming (once again) you set up your initial variables and created a jewishCalendar object, the code to output a Hebrew language date would be:

$hebformat = HebrewDateFormatter::create();
$hebformat->setHebrewFormat(true);
$hebdate = json_decode('"' . $hebformat->format($jewishCalendar) . '"');
print("Hebrew date: $hebdate\n");

which would give you:

Hebrew date: י״ח אלול תשפ״ד

Is today the day?

Sometimes you need to check if a specific date is… well, a specific day. Like Rosh Chodesh (the new month) or part of a multi-day holiday (like Sukkot).

It turns out that PHP Zmanim method makes this very simple. Presuming you’ve started off in the same way as the other examples,

Once you’ve set up the jewishCalendar object it’s as simple as

$jewishCalendar->isRoshHashana();
$jewishCalendar->isSuccos(); 

If the date matches the holiday, it will return “true” and you can proceed with whatever logic or process you want, such as displaying the date (or not).

So what’s next?

Believe it or not, there’s still more to cover. In the coming weeks whenever I get to it, I’d like to cover ways to leverage the built-in astronomy functions for time calculations. Believe it or not, it can really matter in terms of having truly accurate times for things like the start and end of Shabbat and holidays, along with other observances.

In the meanwhile, if there’s something else you’d like to see me cover; or if you have questions, corrections, or kudos, feel free to leave them in the comments below.

版本聲明 本文轉載於:https://dev.to/adatole/time-data-series-the-rest-of-the-story-555h?1如有侵犯,請聯絡[email protected]刪除
最新教學 更多>
  • Bootstrap 4 Beta 中的列偏移發生了什麼事?
    Bootstrap 4 Beta 中的列偏移發生了什麼事?
    Bootstrap 4 Beta:列偏移的刪除和恢復Bootstrap 4 在其Beta 1 版本中引入了重大更改柱子偏移了。然而,隨著 Beta 2 的後續發布,這些變化已經逆轉。 從 offset-md-* 到 ml-auto在 Bootstrap 4 Beta 1 中, offset-md-*...
    程式設計 發佈於2024-11-13
  • 大批
    大批
    方法是可以在物件上呼叫的 fns 數組是對象,因此它們在 JS 中也有方法。 slice(begin):將陣列的一部分提取到新數組中,而不改變原始數組。 let arr = ['a','b','c','d','e']; // Usecase: Extract till index ...
    程式設計 發佈於2024-11-13
  • 如何在 PHP 中組合兩個關聯數組,同時保留唯一 ID 並處理重複名稱?
    如何在 PHP 中組合兩個關聯數組,同時保留唯一 ID 並處理重複名稱?
    在 PHP 中組合關聯數組在 PHP 中,將兩個關聯數組組合成一個數組是常見任務。考慮以下請求:問題描述:提供的代碼定義了兩個關聯數組,$array1 和 $array2。目標是建立一個新陣列 $array3,它合併兩個陣列中的所有鍵值對。 此外,提供的陣列具有唯一的 ID,而名稱可能重疊。要求是建...
    程式設計 發佈於2024-11-13
  • 在 Go 中使用 WebSocket 進行即時通信
    在 Go 中使用 WebSocket 進行即時通信
    构建需要实时更新的应用程序(例如聊天应用程序、实时通知或协作工具)需要一种比传统 HTTP 更快、更具交互性的通信方法。这就是 WebSockets 发挥作用的地方!今天,我们将探讨如何在 Go 中使用 WebSocket,以便您可以向应用程序添加实时功能。 在这篇文章中,我们将介绍: WebSoc...
    程式設計 發佈於2024-11-13
  • 使用 html css 和 javascript 的圖片滑桿 carosual https://www.instagram.com/webstreet_code/
    使用 html css 和 javascript 的圖片滑桿 carosual https://www.instagram.com/webstreet_code/
    ?帶有縮圖和懸停效果的圖像輪播? 嘿,開發社群! ? 在我的最新影片中,我建立了一個優雅的圖像輪播,其縮圖突出顯示具有平滑懸停效果的活動圖像。這種互動式設計增強了使用者參與度,並為您的 Web 專案增添了現代感。 主要特點: 響應式佈局:輪播在所有螢幕尺寸上都能完美調整。 互動式縮圖:可點擊...
    程式設計 發佈於2024-11-12
  • React 的核心:理解元件重新渲染
    React 的核心:理解元件重新渲染
    在學習程式語言時,我們經常深入研究語法並專注於快速建立某些東西,有時會忽略一個關鍵問題:這種語言實際上解決了什麼問題,以及它在幕後如何運作?將我們的注意力轉移到理解語言的核心目的和機制上,可以讓學習速度更快、適應性更強,使我們能夠輕鬆駕馭最複雜的項目。語法總是可以找到的——即使是最經驗豐富的開發人員...
    程式設計 發佈於2024-11-12
  • JavaScript 中的 Deferreds、Promise 和 Future 有什麼區別?
    JavaScript 中的 Deferreds、Promise 和 Future 有什麼區別?
    JavaScript 中 Deferreds、Promise 和 Future 的區別在 JavaScript 中,deferreds、promise 和 futures 通常用於處理非同步操作。這些概念中的每一個都有其獨特的一組特徵:Deferreds在正式文件中從未明確定義,deferreds ...
    程式設計 發佈於2024-11-12
  • 為什麼我的 Web 應用程式中的請求之間沒有維護 Gorilla 會話變數?
    為什麼我的 Web 應用程式中的請求之間沒有維護 Gorilla 會話變數?
    使用 Gorilla 會話時未維護會話變數問題使用 Gorilla Sessions Web 工具包時,會話變數不會跨請求保留。當伺服器啟動並且使用者存取 localhost:8100/ 時,他們將被導向到 login.html,因為會話值不存在。登入後,會話變數將被存儲,並且使用者將被重定向到 h...
    程式設計 發佈於2024-11-12
  • 如何在Python中像“column -t”指令一樣顯示列化資料?
    如何在Python中像“column -t”指令一樣顯示列化資料?
    在 Python 中顯示列式資料在命令列管理工具領域,通常需要以良好對齊的方式呈現資料列。雖然製表符提供了一種簡單的解決方案,但在處理不同長度的資料時它們會失敗。本文旨在透過提出受 Linux「column -t」命令行為啟發的 Python 解決方案來應對這項挑戰。 Python 提供了一個強大的...
    程式設計 發佈於2024-11-12
  • 在 NumPy 數組中尋找特定行的有效方法:問題和解決方案
    在 NumPy 數組中尋找特定行的有效方法:問題和解決方案
    高效查找NumPy 數組中特定行的實例使用NumPy 數組時,可能會遇到需要確定是否array 包含特定行,但ndarray 的標準contains 方法引發了問題。本文針對此問題提出了高效且 Python 的解決方案。 一種方法涉及使用 .tolist() 將 NumPy 數組轉換為 Python...
    程式設計 發佈於2024-11-12
  • 如何解決在伺服器上使用 Matplotlib 的 Python 腳本的「_tkinter.TclError:無顯示名稱且無 $DISPLAY 環境變數」問題?
    如何解決在伺服器上使用 Matplotlib 的 Python 腳本的「_tkinter.TclError:無顯示名稱且無 $DISPLAY 環境變數」問題?
    _tkinter.TclError:沒有顯示名稱,也沒有$DISPLAY 環境變數_tkinter.TclError:沒有顯示名稱,也沒有$DISPLAY 環境變數問題使用Matplotlib 的Python 腳本在伺服器上失敗,並出現錯誤「產生繪圖時沒有顯示名稱和$DISPLAY 環境變數」。出現...
    程式設計 發佈於2024-11-12
  • 如何使用 Apache Commons IO 在 Java 中遞歸刪除目錄?
    如何使用 Apache Commons IO 在 Java 中遞歸刪除目錄?
    在 Java 中遞歸刪除目錄在 Java 中刪除空目錄非常簡單。然而,當處理包含子目錄和檔案的目錄時,該過程變得更加複雜。本文深入探討了使用 Apache Commons IO 函式庫遞歸刪除整個目錄的有效方法。 Apache Commons IO 簡介Apache Commons IO 提供了一套...
    程式設計 發佈於2024-11-12
  • 為什麼即使在同一包中使用 FXML,我的 JavaFX 應用程式也會拋出“Location is required.”錯誤?
    為什麼即使在同一包中使用 FXML,我的 JavaFX 應用程式也會拋出“Location is required.”錯誤?
    JavaFX「需要位置。」儘管FXML 位於同一個套件中仍出現錯誤在JavaFX 應用程式中,遇到「java .lang.NullPointerException: Location is required」錯誤通常表示無法載入FXML 檔案。即使 FXML 檔案與 Application 類別位於...
    程式設計 發佈於2024-11-12
  • `std::enable_if` 是如何運作的:揭開其實現和使用的神秘面紗?
    `std::enable_if` 是如何運作的:揭開其實現和使用的神秘面紗?
    理解std::enable_if:破解其目的和實現理解std::enable_if:破解其目的和實現雖然std::enable_if 的本質是在某些上下文中掌握的,但它的錯綜複雜的問題,特別是模板語句中的第二個參數和對std::enable_if 的賦值,仍然是個謎。深入研究其工作原理將解開這些謎團...
    程式設計 發佈於2024-11-12
  • 如何在 Go 中實作 Python 風格的生成器,同時避免記憶體洩漏?
    如何在 Go 中實作 Python 風格的生成器,同時避免記憶體洩漏?
    Go 中的Python 風格產生器了解通道緩衝區在您的程式碼中,您觀察到增加通道緩衝區大小從1 到10 透過減少上下文切換來增強效能。這個觀念是正確的。較大的緩衝區允許 fibonacci goroutine 提前填充多個點,從而減少 goroutine 之間持續通訊的需要。 通道生命週期和記憶體管...
    程式設計 發佈於2024-11-12

免責聲明: 提供的所有資源部分來自互聯網,如果有侵犯您的版權或其他權益,請說明詳細緣由並提供版權或權益證明然後發到郵箱:[email protected] 我們會在第一時間內為您處理。

Copyright© 2022 湘ICP备2022001581号-3