PHP实现随机日期生成、俄文本地化及周末/节假日判定求助
Hey Anna, let's sort out that weekend and holiday check problem for your PHP date tool. I've put together a complete solution that combines all your requirements, plus fixes a couple of small gaps in your original code:
Complete Working Code
<?php // Generate random timestamp between 2010-01-01 and 2018-01-01 $start = strtotime('2010-01-01'); $end = strtotime('2018-01-01'); $randomTimestamp = rand($start, $end); // English to Russian translation array (fixed missing May/Мая entry) $enToRu = [ // Weekdays 'Sunday' => 'Воскресенье', 'Monday' => 'Понедельник', 'Tuesday' => 'Вторник', 'Wednesday' => 'Среда', 'Thursday' => 'Четверг', 'Friday' => 'Пятница', 'Saturday' => 'Суббота', // Months 'January' => 'Января', 'February' => 'Февраля', 'March' => 'Марта', 'April' => 'Апреля', 'May' => 'Мая', 'June' => 'Июня', 'July' => 'Июля', 'August' => 'Августа', 'September' => 'Сентября', 'October' => 'Октября', 'November' => 'Ноября', 'December' => 'Декабря' ]; // Create English date string (for display and translation) $enDate = date('j F Y l', $randomTimestamp); // Translate to Russian date string $ruDate = str_replace(array_keys($enToRu), array_values($enToRu), $enDate); // Russian holidays array (your original list) $holidays = [ '8 января','7 января','14 января', '23 февраля','8 марта','1 апреля', '12 апреля','1 мая','9 мая', '12 июня','22 июня','1 сентября', '27 сентября','4 ноября','12 декабря', '31 декабря','1 января' ]; // Format date to match holiday array structure (day + Russian month) $enDayMonth = date('j F', $randomTimestamp); $ruDayMonth = str_replace(array_slice(array_keys($enToRu), 7), array_slice(array_values($enToRu), 7), $enDayMonth); // Check if date is weekend or holiday $isWeekend = in_array(date('w', $randomTimestamp), [0, 6]); // 0 = Sunday, 6 = Saturday $isHoliday = in_array($ruDayMonth, $holidays); // Build status label $statusLabels = []; if ($isHoliday) $statusLabels[] = 'holiday'; if ($isWeekend) $statusLabels[] = 'weekend'; $statusText = empty($statusLabels) ? 'workday' : implode(' and ', $statusLabels); // Output in your requested format echo "{$ruDate} ({$enDate}) - {$statusText}"; ?>
Key Fixes & Explanations
- Fixed translation gap: Your original array was missing the May/Мая entry, which would have broken translations for dates in May. I added that in.
- Consistent timestamp usage: All date operations use the same random timestamp, so there's no risk of mismatched dates between translation and checks.
- Weekend check: Uses
date('w')which returns a number (0 = Sunday, 6 = Saturday) — we just check if the value is either 0 or 6. - Holiday check: We convert the random date to the same "day Russian month" format as your
$holidaysarray, then usein_array()to verify if it's a holiday. - Status labeling: Combines holiday/weekend statuses into a clean label (e.g., "holiday and weekend" if both apply, or "workday" if neither).
This will output exactly what you need, like:
2 Февраля 2014 Пятница (2 February 2014 Friday) - workday
1 Января 2015 Четверг (1 January 2015 Thursday) - holiday
7 Января 2018 Воскресенье (7 January 2018 Sunday) - holiday and weekend
内容的提问来源于stack exchange,提问作者Anna Kurmanova
相关产品推荐
相关产品推荐

