Skip to content

Instantly share code, notes, and snippets.

@Zegnat
Created November 5, 2020 15:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Zegnat/70c73fe7758e5149d673caeb0ba615ce to your computer and use it in GitHub Desktop.
Save Zegnat/70c73fe7758e5149d673caeb0ba615ce to your computer and use it in GitHub Desktop.
Quicky to plain text calendar generation
<?php
$tz = new DateTimeZone('UTC');
$now = (new DateTimeImmutable('now', $tz))->setTime(12, 0, 0, 0);
$start = filter_input(INPUT_GET, 'start', FILTER_VALIDATE_REGEXP, ['options' => ['regexp' => '@\d{4,}-\d\d-\d\d@']]);
$end = filter_input(INPUT_GET, 'end', FILTER_VALIDATE_REGEXP, ['options' => ['regexp' => '@\d{4,}-\d\d-\d\d@']]);
if (
is_string($start) &&
($startParts = array_map('intval', explode('-', $start))) &&
checkdate($startParts[1], $startParts[2], $startParts[0])
) {
$start = $now->setDate($startParts[0], $startParts[1], $startParts[2]);
} else {
$start = $now->setDate(intval($now->format('Y')), 1, 1);
}
if (
is_string($end) &&
($endParts = array_map('intval', explode('-', $end))) &&
checkdate($endParts[1], $endParts[2], $endParts[0])
) {
$end = $now->setDate($endParts[0], $endParts[1], $endParts[2]);
} else {
$end = $now->setDate(intval($now->format('Y')), 12, 31);
}
if ($start->diff($end)->invert === 1) {
[$start, $end] = [$end, $start];
}
// Set start to the Monday before:
$start = $start->modify(sprintf('-%d days', intval($start->format('N')) - 1));
// Set end to the Sunday after:
$end = $end->modify(sprintf('+%d days', 7 - intval($end->format('N'))));
$current = $start;
$eol = "\n";
$month = null;
$year = null;
header('Content-Type: text/plain; charset=UTF-8');
echo ' M T W H F S X' . $eol;
echo '----------------------------' . $eol;
if (isset($_GET['notes'])) {
$eol = " ____________________________\n";
}
while ($current->diff($end)->invert === 0) {
if ($current->format('N') === '1') {
echo $current->format('W');
echo ' ';
if ($month !== $current->modify('+6 days')->format('M')) {
$month = $current->modify('+6 days')->format('M');
echo strtoupper($month) . ' ';
} else if ($year !== $current->modify('+6 days')->format('Y')) {
$year = $current->modify('+6 days')->format('Y');
echo $year;
} else {
echo ' ';
}
}
echo ' ' . $current->format('d');
if ($current->format('N') === '7') {
echo $eol;
}
$current = $current->modify('+1 day');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment