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
$month = 2;
$year = 2014;
$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,0,$year);
echo "firstDayOfMonth = $firstDayOfMonth<br/>";
echo "lastDayOfMonth = $lastDayOfMonth<br/>";
$fristWeekDayOfMonth = strftime("%w",$firstDayOfMonth);
$lastWeekDayOfMonth = strftime("%w",$lastDayOfMonth);
$cal=array();
$week=1;
$curDayOfWeek = $fristWeekDayOfMonth;
echo "fristWeekDayOfMonth = $fristWeekDayOfMonth<br/>";
echo "lastWeekDayOfMonth = $lastWeekDayOfMonth<br/>";
for($i = 1; $i < $daysInMonth+1; $i++){
$cal[$i.'-'.$curDayOfWeek] = 1;
$curDayOfWeek = ($curDayOfWeek +1)%7;
}
print_r($cal);
$calFirstWeekDay=1; // Sunday
$calLastWeekDay=6; // Sunday
$calCurWeekDay=$calFirstWeekDay;
$i=1;
$day = 0;
const BEFORE_MONTH = "BEFORE_MONTH";
const IN_MONTH = "IN_MONTH";
const AFTER_MONTH = "AFTER_MONTH";
$cursor = BEFORE_MONTH;
$cal = array();
while($i<50) {
if( $cursor == BEFORE_MONTH && $fristWeekDayOfMonth == $calCurWeekDay) {
$cursor = IN_MONTH;
$day = 0;
}else if( $cursor == AFTER_MONTH && $calCurWeekDay == $calFirstWeekDay) {
break;
}
if( $cursor == IN_MONTH ) {
if( $cursor == IN_MONTH && $day == $daysInMonth ) {
$cursor = AFTER_MONTH;
$day = 0;
}
}
$day++;
$cal[] = array(
'dayOfWeek' => $calCurWeekDay,
'cursor' => $cursor,
'day' => $day
);
$calCurWeekDay = ($calCurWeekDay +1)%7;
$i++;
}
echo '<pre>';
print_r($cal);
echo '</pre>';
?>
<table>
</table>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment