Skip to content

Instantly share code, notes, and snippets.

@joecue
Last active March 9, 2017 17:20
Show Gist options
  • Select an option

  • Save joecue/a5360ddd244dcfc57fa1c046af56c1b8 to your computer and use it in GitHub Desktop.

Select an option

Save joecue/a5360ddd244dcfc57fa1c046af56c1b8 to your computer and use it in GitHub Desktop.
Cleaner Calendar PHP Script
<!DOCTYPE>
<html>
<head>
<style>
#calendar{
width: 336px;
display: inline-block;
background-color: #000;
border: 1px solid #000;
}
.week{
width: 100%;
}
.day{
width: 42px;
height: 42px;
float:left;
padding:2px;
margin: 1px;
background-color: #fff;
}
.current-day{
background-color: #C4C9DF;
}
.day-np{
width: 42px;
height: 42px;
float:left;
background: #e1e1e1;
padding:2px;
margin: 1px;
}
.heading{
background-color: #999;
color: #fff;
text-align: center;
}
.heading-text{
margin: 10px 0 0 0;
}
</style>
</head>
<body>
<?php
$current_month = date("n");
$current_year = date("Y");
$current_date = date("Y") . '-' . date("m") . '-' . date("d");
if(isset($_GET['disp_m'])){
$display_month = $_GET['disp_m'];
$display_year = $_GET['disp_y'];
} else {
$display_month = $current_month;
$display_year = $current_year;
}
$next_month = $display_month;
$next_year = $display_year;
$prev_month = $display_month;
$prev_year = $display_year;
if ($display_month == '12'){
$next_month = 1;
$next_year = date("Y")+1;
$prev_month -=1;
} elseif($display_month > '12'){
$next_month = 1;
$next_year = date("Y")+1;
$prev_month = 12;
} elseif($display_month < '12'){
$next_month +=1;
$prev_month -=1;
if ($prev_month == 0){
$prev_month = 12;
$prev_year -=1;
}
}
//echo 'Current Month: ' . $current_month . ' - ' . $current_year . '<br /><br />';
//echo 'Display Month: ' . $display_month . ' - ' . $display_year . '<br /><br />';
//echo 'Next Month: ' . $next_month . ' - ' . $next_year . '<br/><br />';
//echo 'Prev Month: ' . $prev_month . ' - ' . $prev_year . '<br/><br />';
$calendar = '<div id="calendar">';
$headings = array('Sun','Mon','Tues','Wed','Thurs','Fri','Sat');
$calendar .= '<div class="week heading"><div class="day heading"><div class="heading-text">'.implode('</div></div><div class="day heading"><div class="heading-text">', $headings).'</div></div></div>';
$running_day = date('w',mktime(0,0,0,$display_month,1,$display_year));
$days_in_month = date('t',mktime(0,0,0,$display_month,1,$display_year));
$days_in_this_week = 1;
$day_counter = 0;
$dates_array = array();
$calendar.= '<div class="week">';
for($x = 0; $x < $running_day; $x++):
$calendar.= '<div class="day-np">&nbsp;</div>';
$days_in_this_week++;
endfor;
for($list_day = 1; $list_day <= $days_in_month; $list_day++):
// determine if day is single digit or two, and transform to two digits
if (strlen($list_day) == 1){
$day_of_week = '0' . $list_day;
}else{
$day_of_week = $list_day;
}
// determine if month is single digit or two, and transform to two digits
if (strlen($display_month) == 1){
$full_month = '0' . $display_month;
}else{
$full_month = $display_month;
}
// fully formed date to match WordPress date variable
$full_date = $display_year . '-' . $full_month . '-' . $day_of_week;
if($full_date == $current_date){
$calendar.= '<div class="day current-day">';
$calendar.= '<div class="day-number">'.$list_day.'</div>';
$calendar.= '</div>';
}else{
$calendar.= '<div class="day">';
$calendar.= '<div class="day-number">'.$list_day.'</div>';
$calendar.= '</div>';
}
if($running_day == 6):
$calendar.= '</div>';
if(($day_counter+1) != $days_in_month):
$calendar.= '<div class="week">';
endif;
$running_day = -1;
$days_in_this_week = 0;
endif;
$days_in_this_week++; $running_day++; $day_counter++;
endfor;
if($days_in_this_week < 8):
for($x = 1; $x <= (8 - $days_in_this_week); $x++):
$calendar.= '<div class="day-np">&nbsp;</div>';
endfor;
endif;
if ($prev_month < $current_month && $prev_year == $current_year){
echo '<div style="width:336px;"><div style="width: 33%; float:left;">&nbsp;</div><div style="width: 33%; float:left;text-align:center;" class="cur-month">' . date('F', mktime(0, 0, 0, $display_month, 10)) . ' ' . $display_year . '</div><div style="width: 33%;float:left; text-align:right;"><a href="?disp_m=' . $next_month . '&disp_y=' . $next_year . '">Next Month</a></div></div><div style="clear:both;"></div>';
}elseif($prev_month == $current_month && $prev_year == $current_year){
echo '<div style="width:336px;"><div style="width: 33%;float:left;text-align:left;"><a href="?disp_m=' . $prev_month . '&disp_y=' . $prev_year . '">Previous Month</a></div><div style="width: 33%;float:left;text-align:center;" class="cur-month">' . date('F', mktime(0, 0, 0, $display_month, 10)) . ' ' . $display_year . '</div><div style="width: 33%;float:left; text-align:right;"><a href="?disp_m=' . $next_month . '&disp_y=' . $next_year . '">Next Month</a></div></div><div style="clear:both;"></div>';
}else{
echo '<div style="width:336px;"><div style="width: 33%;float:left;text-align:left;"><a href="?disp_m=' . $prev_month . '&disp_y=' . $prev_year . '">Previous Month</a></div><div style="width: 33%;float:left;text-align:center;" class="cur-month">' . date('F', mktime(0, 0, 0, $display_month, 10)) . ' ' . $display_year . '</div><div style="width: 33%;float:left; text-align:right;"><a href="?disp_m=' . $next_month . '&disp_y=' . $next_year . '">Next Month</a></div></div><div style="clear:both;"></div>';
}
echo $calendar;
?>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment