From 819a5ff5fdcdf178094c6af7e84cc2fbc5dad0a9 Mon Sep 17 00:00:00 2001 From: Nils Date: Sun, 24 Nov 2024 20:04:38 +0100 Subject: [PATCH] new way to calculate the third wednesday --- index.php | 8 +-- third_wednesday_new.php | 121 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 125 insertions(+), 4 deletions(-) create mode 100644 third_wednesday_new.php diff --git a/index.php b/index.php index 3a404b6..61443ee 100644 --- a/index.php +++ b/index.php @@ -1,7 +1,7 @@ @@ -186,7 +186,7 @@ $followingDates = listNextElevenThirdWendesdays();

Jeder Dritte Mittwoch im Monat (außer Dezember):

- +

Diese Kalenderdatei kann importiert/abonniert werden und enthält fortlaufend alle regelmäßigen Termine.

diff --git a/third_wednesday_new.php b/third_wednesday_new.php new file mode 100644 index 0000000..20f5c57 --- /dev/null +++ b/third_wednesday_new.php @@ -0,0 +1,121 @@ +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"; +} +?>