Skip to content

Instantly share code, notes, and snippets.

@raoul2000
Last active August 29, 2015 13:56
Show Gist options
  • Save raoul2000/9133730 to your computer and use it in GitHub Desktop.
Save raoul2000/9133730 to your computer and use it in GitHub Desktop.
create calendar
<?php
$dayNames = array(
0 => "Sunday",
1 => "Monday",
2 => "Tuesday",
3 => "Wednesday",
4 => "Thursday",
5 => "Friday",
6 => "Saturday"
);
$month = 1;
$year = 2014;
echo "month = $month<br/>";
$daysInMonth = cal_days_in_month (CAL_GREGORIAN , $month , $year );
$firstDayOfMonth = mktime(0,0,0,$month,1,$year); // 0 = Sunday, 1 = Monday, etc ...
$lastDayOfMonth = mktime(0,0,0,$month,$daysInMonth,$year);
$lastDayOfPrevMonth = mktime(0,0,0,$month,0,$year);
echo "firstDayOfMonth = $firstDayOfMonth<br/>";
echo "lastDayOfMonth = $lastDayOfMonth<br/>";
$fristWeekDayOfMonth = strftime("%w",$firstDayOfMonth);
$lastWeekDayOfMonth = strftime("%w",$lastDayOfMonth);
$countDaysInPrevMonth = strftime("%d",$lastDayOfPrevMonth);
$cal=array();
$week=1;
$curDayOfWeek = $fristWeekDayOfMonth;
echo "fristWeekDayOfMonth = $fristWeekDayOfMonth<br/>";
echo "lastWeekDayOfMonth = $lastWeekDayOfMonth<br/>";
echo "countDaysInPrevMonth = $countDaysInPrevMonth<br/>";
$calFirstWeekDay=1;
$calLastWeekDay= ($calFirstWeekDay + 6)%7;
echo "calFirstWeekDay = $calFirstWeekDay<br/>";
echo "calLastWeekDay = $calLastWeekDay<br/>";
$daysBefore = abs($fristWeekDayOfMonth - $calFirstWeekDay);
if( $calFirstWeekDay > $fristWeekDayOfMonth){
$r = 7 - abs($fristWeekDayOfMonth - $calFirstWeekDay);
} else {
$r = abs($fristWeekDayOfMonth - $calFirstWeekDay);
}
$daysBefore = $r;
echo "daysBefore = $daysBefore<br/>";
$daysAfter = abs( $lastWeekDayOfMonth - $calLastWeekDay);
if( $lastWeekDayOfMonth > $calLastWeekDay) $daysAfter = 7- $daysAfter;
echo "daysAfter = $daysAfter<br/>";
$calTotalDays = $daysBefore + $daysInMonth + $daysAfter;
echo "calTotalDays = $calTotalDays<br/>";
$calCurWeekDay=$calFirstWeekDay;
$day = $countDaysInPrevMonth - $daysBefore;
const BEFORE_MONTH = "BEFORE_MONTH";
const IN_MONTH = "IN_MONTH";
const AFTER_MONTH = "AFTER_MONTH";
$cursor = BEFORE_MONTH;
$cal = array();
$i = $calTotalDays;
$calendar = array();
$weekIdx = 0;
while($i > 0) {
if( $cursor == BEFORE_MONTH && $fristWeekDayOfMonth == $calCurWeekDay) {
$cursor = IN_MONTH;
$day = 0;
}
else if( $cursor == IN_MONTH && $day == $daysInMonth ) {
$cursor = AFTER_MONTH;
$day = 0;
}
else if( $cursor == AFTER_MONTH && $calCurWeekDay == $calFirstWeekDay) {
break;
}
if( $calCurWeekDay == $calFirstWeekDay)
$weekIdx++;
$day++;
$dayItem = array(
'dayOfWeek' => $calCurWeekDay,
'cursor' => $cursor,
'day' => $day
);
$cal[] = $dayItem;
$calendar[$weekIdx][] = $dayItem;
$calCurWeekDay = ($calCurWeekDay +1)%7;
$i--;
}
?>
<hr/>
<table width="100%" border="1">
<caption>
Caption
</caption>
<thead>
<tr>
<?php foreach( $calendar[1] as $calWeek ):?>
<th><?php echo $dayNames[$calWeek['dayOfWeek']] . "(" .$calWeek['dayOfWeek'] .")"; ?></th>
<?php endforeach; ?>
</tr>
</thead>
<tbody>
<?php foreach( $calendar as $calWeek ):?>
<tr>
<?php foreach( $calWeek as $calDay ) :?>
<td><?php echo $calDay['day']; ?></td>
<?php endforeach; ?>
</tr>
<?php endforeach; ?>
</tbody>
<tfoot>
<tr>
<td colspan="7">footer</td>
</tr>
</tfoot>
</table>
<?php
echo '<pre>';
print_r($calendar);
echo '</pre>';
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment