Skip to content

Instantly share code, notes, and snippets.

@hussaintamboli
Created March 14, 2014 05:47
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 hussaintamboli/9542711 to your computer and use it in GitHub Desktop.
Save hussaintamboli/9542711 to your computer and use it in GitHub Desktop.
A Calendar in PHP. Kind of looks like Github's activity calendar but vertical.
<?php
$year = '2014';
$start = date("$year-01-01");
$end = date("$year-12-t");
$week = array();
$month = array();
$i = 0;
print_r("Calendar: $year\n\n");
do {
$week['start'] = $start;
$date = strtotime($start);
$d = date("D", $date);
if ($d == "Sat")
$weekend = $date;
else if ($d == "Sun")
$weekend = strtotime($start . " +6 days");
else{
$weekend = strtotime($start . " next saturday");
}
$weekend = date("Y-m-d", $weekend);
$week['end'] = $weekend;
array_push($month, $week);
$start = strtotime($weekend . " +1 days");
$start = date("Y-m-d", $start);
$i++;
} while(strtotime($start) <= strtotime($end));
$month[$i - 1]['end'] = $end;
//print_r($month);
foreach ($month as $week) {
$start = $week['start'];
$end = $week['end'];
$begin = new DateTime($start);
$end = strtotime($end . " +1 days");
$end = new DateTime(date("Y-m-d", $end));
$interval = DateInterval::createFromDateString('1 day');
$period = new DatePeriod($begin, $interval, $end);
$d = date('D', strtotime($start));
if ($d != 'Sun') {
$day = strtotime($start);
$lastSun = strtotime($start . ' last sunday');
$datediff = $day - $lastSun;
$difference = floor($datediff/(60*60*24));
echo str_repeat(' ', $difference);
}
foreach ($period as $dt) {
$day = $dt->format("d");
print $day . " ";
}
print "\n";
}
@hussaintamboli
Copy link
Author

Output :

Calendar: 2014
.............01 02 03 04
05 06 07 08 09 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31 01
02 03 04 05 06 07 08
09 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 01
02 03 04 05 06 07 08
09 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 01 02 03 04 05
06 07 08 09 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 01 02 03
04 05 06 07 08 09 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
01 02 03 04 05 06 07
08 09 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 01 02 03 04 05
06 07 08 09 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31 01 02
03 04 05 06 07 08 09
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31 01 02 03 04 05 06
07 08 09 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 01 02 03 04
05 06 07 08 09 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31 01
02 03 04 05 06 07 08
09 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 01 02 03 04 05 06
07 08 09 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