Skip to content

Instantly share code, notes, and snippets.

@elzup
Created December 17, 2014 09:02
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 elzup/61b598d7b0aa520c1d03 to your computer and use it in GitHub Desktop.
Save elzup/61b598d7b0aa520c1d03 to your computer and use it in GitHub Desktop.
PHPでカレンダー出力 ref: http://qiita.com/elzup/items/66a51dad716536aff159
<?php
$col_width = 3;
$title_format = '-*- Y-m -*-';
// 引数がない場合は今月
$ts = strtotime("first day of");
if (isset($argv[2])) {
$ts = strtotime("first day of {$argv[1]}-{$argv[2]}");
}
list($y, $m, $t, $w) = explode(',', date('Y,m,t,w', $ts));
// title
$title = str_replace(array('Y', 'm'), array($y, sprintf('%2d', $m)), $title_format);
echo str_repeat(' ', ($col_width * 7 - strlen($title)) / 2) . $title . PHP_EOL;
echo str_repeat(' ', $w * $col_width);
for ($i = 1; $i <= $t; $i++) {
echo sprintf("%{$col_width}d", $i);
if (($w + $i) % 7 == 0) {
echo PHP_EOL;
}
}
echo PHP_EOL;
-*- 2014-12 -*-
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