הצגת תאריך עברי בוורדפרס
תוכן עניינים
הקוד נועד להציג תאריך עברי באתר WordPress והוא משתמש ביכולות המובנות של PHP לתמיכה בלוחות שנה שונים.
איך הקוד עובד?
הקוד עושה שימוש במחלקה IntlDateFormatter, שהיא חלק מהרחבה בינלאומית (intl) של PHP, המאפשרת עבודה עם תאריכים בפורמטים שונים ולוחות שנה מגוונים, כולל לוח השנה העברי.
הגדרת לוח שנה עברי: באמצעות ‘he_IL@calendar=hebrew’, הקוד מגדיר שהפורמט הרצוי הוא עברי, בשפה העברית, עם אזור הזמן המתאים (Asia/Jerusalem).
ניתן להגדיר את פורמט התצוגה. לדוגמה:
IntlDateFormatter::FULL – תאריך מלא כולל יום, חודש ושנה.
MMMM – תצוגה של שם החודש בלבד (לדוגמה: “תשרי”).
ניתן להתאים את התבנית לפי הצורך.
הצגת תאריך עברי מלא, יום תאריך וחודש
הוסיפו את הקוד לקובץ functions.php של תבנית הבת או באמצעות תוסף לניהול סניפטים (Snippets)
function display_hebrew_date() {
$formatter = new IntlDateFormatter(
'he_IL@calendar=hebrew',
IntlDateFormatter::FULL,
IntlDateFormatter::NONE,
'Asia/Jerusalem',
IntlDateFormatter::TRADITIONAL
);
$hebrew_date = $formatter->format(new DateTime());
return $hebrew_date;
}
add_shortcode('hebrew_date', 'display_hebrew_date');
עכשיו השתמשו בשורטקוד הבא בכל מקום בו תרצו להציג את התאריך
[hebrew_date]
הצגת החודש העברי
הוסיפו את הקוד לקובץ functions.php של תבנית הבת או באמצעות תוסף לניהול סניפטים (Snippets)
function display_hebrew_month() {
$formatter = new IntlDateFormatter(
'he_IL@calendar=hebrew',י
IntlDateFormatter::NONE,
IntlDateFormatter::NONE,
'Asia/Jerusalem',
IntlDateFormatter::TRADITIONAL
);
$formatter->setPattern('MMMM');
$hebrew_month = $formatter->format(new DateTime());
return $hebrew_month;
}
add_shortcode('hebrew_month', 'display_hebrew_month');
עכשיו השתמשו בשורטקוד הבא בכל מקום בו תרצו להציג את החודש העברי
[hebrew_month]
הצגת התאריך העברי לשרתים שלא תומכים בעברית
כולל חישוב והמרה. הוסיפו את הקוד לקובץ functions.php של תבנית הבת או באמצעות תוסף לניהול סניפטים (Snippets)
function hebrew_number_to_words($number, $is_year = false) {
$hebrew_letters = [
1 => 'א', 2 => 'ב', 3 => 'ג', 4 => 'ד', 5 => 'ה', 6 => 'ו', 7 => 'ז', 8 => 'ח', 9 => 'ט',
10 => 'י', 20 => 'כ', 30 => 'ל', 40 => 'מ', 50 => 'נ', 60 => 'ס', 70 => 'ע', 80 => 'פ', 90 => 'צ',
100 => 'ק', 200 => 'ר', 300 => 'ש', 400 => 'ת'
];
$result = '';
if ($is_year) {
$result .= 'תש';
$last_two_digits = $number % 100;
if ($last_two_digits <= 10 || $last_two_digits % 10 == 0) {
$result .= $hebrew_letters[$last_two_digits];
} else {
$tens = intdiv($last_two_digits, 10) * 10;
$units = $last_two_digits % 10;
$result .= $hebrew_letters[$tens] . '"' . $hebrew_letters[$units];
}
} else {
$hundreds = intdiv($number, 100);
if ($hundreds > 0) {
$result .= $hebrew_letters[$hundreds * 100];
$number %= 100;
}
if ($number > 10 && $number <= 19) {
$result .= 'ט' . $hebrew_letters[$number - 10];
return $result;
}
$tens = intdiv($number, 10);
if ($tens > 0) {
$result .= $hebrew_letters[$tens * 10];
$number %= 10;
}
if ($number > 0) {
$result .= $hebrew_letters[$number];
}
}
return $result;
}
function display_hebrew_date() {
$formatter = new IntlDateFormatter(
'he_IL@calendar=hebrew',
IntlDateFormatter::FULL,
IntlDateFormatter::NONE,
'Asia/Jerusalem',
IntlDateFormatter::TRADITIONAL
);
$hebrew_date = $formatter->format(new DateTime());
$days_map = [
'Sunday' => 'יום ראשון',
'Monday' => 'יום שני',
'Tuesday' => 'יום שלישי',
'Wednesday' => 'יום רביעי',
'Thursday' => 'יום חמישי',
'Friday' => 'יום שישי',
'Saturday' => 'שבת',
];
$months_map = [
'Tishri' => 'תשרי',
'Heshvan' => 'חשוון',
'Kislev' => 'כסלו',
'Tevet' => 'טבת',
'Shevat' => 'שבט',
'Adar' => 'אדר',
'Adar I' => 'אדר א׳',
'Adar II' => 'אדר ב׳',
'Nisan' => 'ניסן',
'Iyyar' => 'אייר',
'Sivan' => 'סיוון',
'Tamuz' => 'תמוז',
'Av' => 'אב',
'Elul' => 'אלול',
];
$hebrew_date = str_replace(array_keys($days_map), array_values($days_map), $hebrew_date);
$hebrew_date = str_replace(array_keys($months_map), array_values($months_map), $hebrew_date);
$current_date = new DateTime();
$hebrew_year = intval($current_date->format('Y')) + 3760;
$hebrew_day = intval($current_date->format('j'));
$hebrew_year_words = hebrew_number_to_words($hebrew_year, true);
$hebrew_day_words = hebrew_number_to_words($hebrew_day);
$hebrew_date = preg_replace('/\d{4}/', $hebrew_year_words, $hebrew_date);
$hebrew_date = preg_replace('/\d{1,2}/', $hebrew_day_words, $hebrew_date);
return $hebrew_date;
}
add_shortcode('hebrew_date', 'display_hebrew_date');
עכשיו השתמשו בשורטקוד הבא בכל מקום בו תרצו להציג את החודש העברי
[hebrew_month]