Skip to content

Instantly share code, notes, and snippets.

@MrCyjaneK
Last active July 13, 2020 18:47
Show Gist options
  • Save MrCyjaneK/17e56aa5fda882fb6e4533d9de9c55f8 to your computer and use it in GitHub Desktop.
Save MrCyjaneK/17e56aa5fda882fb6e4533d9de9c55f8 to your computer and use it in GitHub Desktop.
Generate calendar in php
<?php
// My target is to create script to generate something like this:
//
// ------------------------
// | January |
// | s m t w t f s |
// | 1 2 3 4 |
// | 5 6 7 8 9 10 11 |
// | 12 13 14 15 16 17 18 |
// | 19 20 21 22 23 24 25 |
// | 26 27 28 29 30 31 |
// ------------------------
//
//
// $startingDay - number 0 to 7
// $days - how many days to render
// $month - Name of month
function createCalendar($startingDay, $days, $month) {
$spaces = ' ';
$out = [];
$out[] = '------------------------'; // length is 24
$out[] = '| '.substr($month.$spaces,0,20).' |';
$out[] = '| s m t w t f s |';
$calday = 0;
$day = $startingDay*-1+1;
while ($day <= $days) {
if ($day <= 0) {
$dtp = ' ';
} else {
$dtp = substr("$day ",0,3);
}
if ($calday % 7 == 0) {
$out[] .= "| $dtp";
} else if ($calday % 7 == 6) {
$out[count($out)-1] .= "$dtp|";
} else {
$out[count($out)-1] .= $dtp;
}
$day++;
$calday++;
}
//$out[] = '------------------------';
while (strlen($out[count($out)-1]) < 23) {
echo "";
$out[count($out)-1] .= ' ';
}
$out[count($out)-1] .= '|';
$out[] = '------------------------';
return implode($out,PHP_EOL);
}
echo createCalendar($argv[1], $argv[2], $argv[3]);
@MrCyjaneK
Copy link
Author

MrCyjaneK commented Jul 13, 2020

cyjan@ubudubu ~/P/phpCalendar> php index.php 1 31 January
------------------------
| January              |
| s  m  t  w  t  f  s  |
|    1  2  3  4  5  6  |
| 7  8  9  10 11 12 13 |
| 14 15 16 17 18 19 20 |
| 21 22 23 24 25 26 27 |
| 28 29 30 31          |
------------------------⏎```

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment