Skip to content

Instantly share code, notes, and snippets.

@m1roff
Created November 2, 2023 20:55
Show Gist options
  • Save m1roff/773b67b8afafbb953a132b0ef5cf2fe2 to your computer and use it in GitHub Desktop.
Save m1roff/773b67b8afafbb953a132b0ef5cf2fe2 to your computer and use it in GitHub Desktop.
Labeling: PHP-Script-to-Iterate-Over-Days-of-Month-with-German-Names
<?php
$forMonth = '2023-11-01';
$months = [
1 => 'Янв', 2 => 'Фев', 3 => 'Мар', 4 => 'Апр',
5 => 'Май', 6 => 'Июн', 7 => 'Июл', 8 => 'Авг',
9 => 'Сен', 10 => 'Окт', 11 => 'Ноя', 12 => 'Дек'
];
$days = [
1 => 'Пн', 2 => 'Вт', 3 => 'Ср', 4 => 'Чт',
5 => 'Пт', 6 => 'Сб', 7 => 'Вс'
];
$months = [
1 => 'Jan', 2 => 'Feb', 3 => 'Mär', 4 => 'Apr',
5 => 'Mai', 6 => 'Jun', 7 => 'Jul', 8 => 'Aug',
9 => 'Sep', 10 => 'Okt', 11 => 'Nov', 12 => 'Dez'
];
$days = [
1 => 'Mo', 2 => 'Di', 3 => 'Mi', 4 => 'Do',
5 => 'Fr', 6 => 'Sa', 7 => 'So'
];
$dateStart = new DateTimeImmutable($forMonth); // Начальная дата
$dateEnd = $dateStart->modify('first day of next month'); // Конец месяца
$period = new DatePeriod($dateStart, new DateInterval('P1D'), $dateEnd);
$printFormat = '{day} {m}. {year} ({dayOfWeek})';
foreach ($period as $date) {
$dayOfWeek = $days[$date->format('N')];
$month = $months[$date->format('n')];
$day = $date->format('d');
if ($date->format('N') === '7') {
continue;
}
if ($date->format('N') === '6') {
$sunday = $date->modify('+1 day');
$day .= '/' . $sunday->format('d');
$dayOfWeek .= '/' . $days[$sunday->format('N')];
}
echo strtr($printFormat, [
'{day}' => $day,
'{m}' => $month,
'{year}' => $date->format('y'),
'{dayOfWeek}' => $dayOfWeek,
]) . PHP_EOL;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment