Created
March 14, 2014 05:47
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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