new way to calculate the third wednesday

This commit is contained in:
Nils 2024-11-24 20:04:38 +01:00
parent 59f1295bea
commit 819a5ff5fd
2 changed files with 125 additions and 4 deletions

View File

@ -1,7 +1,7 @@
<?php <?php
include "third_wednesday.php"; include "third_wednesday_new.php";
$nextDate = nextThirdWednesday(); $nextDate = getNextThirdWednesday();
$followingDates = listNextElevenThirdWendesdays(); $followingDates = getNextElevenThirdWednesdays();
?> ?>
<!doctype html> <!doctype html>

121
third_wednesday_new.php Normal file
View File

@ -0,0 +1,121 @@
<?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";
}
?>