osamc.de/third_wednesday_new.php

122 lines
3.5 KiB
PHP
Raw Normal View History

<?php
function getNextThirdWednesday() {
// Set the timezone to Europe/Berlin
$timezone = new DateTimeZone('Europe/Berlin');
$today = new DateTime('now', $timezone);
$month = (int)$today->format('n');
$year = (int)$today->format('Y');
// Skip December
if ($month == 12) {
$month = 1;
$year++;
}
// Find the third Wednesday of the current month
$thirdWednesdayThisMonth = new DateTime("third Wednesday of $year-$month", $timezone);
// Check if today is before or on the third Wednesday
if ($today <= $thirdWednesdayThisMonth && (int)$thirdWednesdayThisMonth->format('n') == $month) {
if ($today->format('Y-m-d') == $thirdWednesdayThisMonth->format('Y-m-d')) {
// Today is the third Wednesday
return $today->format('d.m.y');
} else {
// Today is before the third Wednesday
return $thirdWednesdayThisMonth->format('d.m.y');
}
} else {
// Move to the next month
$month++;
if ($month == 12) {
$month = 1;
$year++;
} elseif ($month > 12) {
$month = 1;
$year++;
}
// Skip December
if ($month == 12) {
$month = 1;
$year++;
}
// Find the third Wednesday of the next month
$thirdWednesdayNextMonth = new DateTime("third Wednesday of $year-$month", $timezone);
return $thirdWednesdayNextMonth->format('d.m.y');
}
}
function getNextElevenThirdWednesdays() {
// Set the timezone to Europe/Berlin
$timezone = new DateTimeZone('Europe/Berlin');
// Get the next third Wednesday from the first function
$nextThirdWednesdayStr = getNextThirdWednesday();
$nextThirdWednesdayDate = DateTime::createFromFormat('d.m.y', $nextThirdWednesdayStr, $timezone);
$dates = array();
$month = (int)$nextThirdWednesdayDate->format('n');
$year = (int)$nextThirdWednesdayDate->format('Y');
// Start from the month after the next third Wednesday date
$month++;
if ($month == 12) {
$month = 1;
$year++;
} elseif ($month > 12) {
$month = 1;
$year++;
}
$count = 0;
while ($count < 11) {
// Skip December
if ($month == 12) {
$month++;
if ($month > 12) {
$month = 1;
$year++;
}
continue;
}
// Get the third Wednesday of this month
$thirdWednesday = new DateTime("third Wednesday of $year-$month", $timezone);
// Exclude the date from the first function
if ($thirdWednesday->format('Y-m-d') == $nextThirdWednesdayDate->format('Y-m-d')) {
$month++;
if ($month > 12) {
$month = 1;
$year++;
}
continue;
}
// Collect the date in DD.MM format
$dates[] = $thirdWednesday->format('d.m');
$count++;
// Increment month
$month++;
if ($month == 12) {
$month++;
}
if ($month > 12) {
$month = 1;
$year++;
}
}
// Return the dates as a string separated by spaces
return implode(' ', $dates);
}
// Execute the usage part only if the script is run directly
if (__FILE__ == realpath($_SERVER['SCRIPT_FILENAME'])) {
echo "Next third Wednesday (excluding December): " . getNextThirdWednesday() . "\n";
echo "Next 11 third Wednesdays (excluding December): " . getNextElevenThirdWednesdays() ."\n";
}
?>