You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
72 lines
2.5 KiB
72 lines
2.5 KiB
<?php |
|
// read vor.txt and generate vor.md, inserting dates if missing |
|
// Helper function for next Tuesday calculation depending on PHP version |
|
function getTuesday($ts) { |
|
// This is for PHP >= 8.1 as the old strftime will be deprecated by then, so use datefmt_format_object if we are running on PHP beyond version 7 |
|
if (intval(explode(".", phpversion())[0]) >= 7) { |
|
$date_time = new DateTime(); |
|
$date_time->setTimestamp(strtotime('fourth tuesday of this month', $ts)); |
|
$tuesday = datefmt_format_object($date_time, 'cccc, d.M.yyyy ', 'de_DE.utf8'); |
|
} else { |
|
$tuesday = strftime('%A, %d.%m.%Y ', strtotime('fourth tuesday of this month', $ts)); |
|
} |
|
return $tuesday; |
|
} |
|
|
|
date_default_timezone_set("CET"); |
|
setlocale(LC_TIME, 'de_DE.utf8'); |
|
$file_txt = 'vor.txt'; |
|
$file_md = 'vor.md'; |
|
$fil = file($file_txt, FILE_IGNORE_NEW_LINES); |
|
// Insert header |
|
$arr = ['| | | |', '|:------|:------|:------|']; |
|
$first_day = new DateTime('first day of this month'); |
|
$ts = $first_day->getTimeStamp(); |
|
$first_wedn = new DateTime('first wednesday of this month'); |
|
$number = 'fourth'; |
|
// Treat March differently |
|
if ($first_wedn->format('md') == '0301') { |
|
$number = 'fifth'; |
|
} |
|
$tues_ts = strtotime($number . ' tuesday of this month', $ts); |
|
$tues_date = (new DateTime())->setTimeStamp($tues_ts); |
|
|
|
const JUST_DATE = 'Ymd'; |
|
|
|
// Are we past the fourth Tuesday of this month? |
|
if ((new DateTime())->getTimeStamp() >= $tues_ts && $first_day->format(JUST_DATE) != $first_wedn->format(JUST_DATE) && $tues_date->format('md') != '0322') { |
|
$ts = strtotime('+1 month', $ts); |
|
/* |
|
var_dump($tues_date->format('md')); |
|
var_dump($first_day); |
|
var_dump($first_wedn); |
|
var_dump($ts); |
|
*/ |
|
} |
|
|
|
foreach ($fil as $line) { |
|
// Skip empty lines |
|
if (strlen($line)) { |
|
// Remove empty fields too |
|
$fields = array_filter(explode('|', $line)); |
|
// Get next Tuesday independent of PHP version |
|
$tuesday = getTuesday($ts); |
|
if (count($fields) == 2) { |
|
// Date missing, insert |
|
array_unshift($fields, $tuesday); |
|
} |
|
$ts = strtotime('+1 month', $ts); |
|
$li = implode('|', $fields); |
|
// Insert leading and trailing spaces if required |
|
if ($li[0] != ' ') { |
|
$li = ' ' . $li; |
|
} |
|
if (substr($li, -1) != ' ') { |
|
$li = $li . ' '; |
|
} |
|
array_push($arr, '|' . $li . '|'); |
|
} |
|
} |
|
file_put_contents($file_md, implode(PHP_EOL, $arr) . PHP_EOL); |
|
?> |
|
|
|
|