Jump to content

My first calendar - can it be optimised?


Yesideez

Recommended Posts

I've never written a calendar before and was wondering if it can be optimised at all. I've used DIVs throughout to build it.

 

Here's the PHP:

<?php
$arrDays=array(1=>'Monday',2=>'Tuesday',3=>'Wednesday',4=>'Thursday',5=>'Friday',6=>'Saturday',7=>'Sunday');
$arrMonths=array(1=>'January',2=>'February',3=>'March',4=>'April',5=>'May',6=>'June',7=>'July',8=>'August',9=>'September',10=>'October',11=>'November',12=>'December',7=>'Sunday');
//GET TODAY'S DATE
$intCurrentDay=date('j',time());
$intCurrentMonth=date('n',time());
$intCurrentYear=date('Y',time());
//GET FIRST DAY OF THE WEEK
$intStartDay=date('N',mktime(0,0,0,$intCurrentMonth,1,$intCurrentYear));
//GET DAYS IN CURRENT MONTH
$intDaysCurrentMonth=date('t',mktime(0,0,0,$intCurrentMonth,1,$intCurrentYear));
//GET THE NUMBER OF DAYS IN PREVIOUS MONTH 
if ($intCurrentMonth-1<1) {
$intPreviousMonth=12;
$intPreviousYear=$intCurrentYear-1;
} else {
$intPreviousMonth=$intCurrentMonth-1;
$intPreviousYear=$intCurrentYear;
}
$intDaysPreviousMonth=date('t',mktime(0,0,0,$intPreviousMonth,1,$intPreviousYear));
//GET THE NUMBER OF DAYS IN NEXT MONTH 
if ($intCurrentMonth+1>12) {
  $intNextMonth=1;
  $intNextYear=$intCurrentYear+1;
} else {
  $intNextMonth=$intCurrentMonth+1;
  $intNextYear=$intCurrentYear;
}
$intDaysNextMonth=date('t',mktime(0,0,0,$intNextMonth,1,$intNextYear));
//CALCULATE START DAY OF NEXT MONTH
$intStartDayNextMonth=date('N',mktime(0,0,0,$intNextMonth,1,$intNextYear));

?>
<html>
<head>
  <title>Calendar</title>
  <link rel="stylesheet" type="text/css" href="calendar.css">
</head>
<body>
  <div id="cal_wrapper">
    <div id="cal_days" class="cal_day">Mo</div>
    <div id="cal_days" class="cal_day">Tu</div>
    <div id="cal_days" class="cal_day">We</div>
    <div id="cal_days" class="cal_day">Th</div>
    <div id="cal_days" class="cal_day">Fr</div>
    <div id="cal_days_weekend">Sa</div>
    <div id="cal_days_weekend">Su</div>
<?php
$dayCounter=1;
//DAYS OF THE PREVIOUS MONTH
if ($intStartDay>1) {
for ($i=($intDaysPreviousMonth-$intStartDay+2);$i<=$intDaysPreviousMonth;++$i) {
	echo '<div id="cal_days" class="ghosted'.($dayCounter==6||$dayCounter==7 ? ' cal_weekend' : ' cal_weekday').'">'.$i.'</div>';
	$dayCounter++;
    if ($dayCounter== {$dayCounter=1;}
}
}
//DAYS OF THE CURRENT MONTH
for ($i=1;$i<=$intDaysCurrentMonth;++$i) {
echo '<div id="cal_days" class="'.($dayCounter==6||$dayCounter==7 ? ' cal_weekend' : ' cal_weekday').'">'.$i.'</div>';
  $dayCounter++;
  if ($dayCounter== {$dayCounter=1;}
}
//DAYS OF THE NEXT MONTH
$loopEnd=8-$intStartDayNextMonth;
for ($i=1;$i<=$loopEnd;++$i) {
echo '<div id="cal_days" class="ghosted'.($dayCounter==6||$dayCounter==7 ? ' cal_weekend' : ' cal_weekday').'">'.$i.'</div>';
  $dayCounter++;
  if ($dayCounter== {$dayCounter=1;}
}
?>
  </div>
</body>
</html>

 

Here's the CSS:

#cal_wrapper {
  width: 434px;
  background-color: #ff0000;
}
#cal_days {
  width: 60px;
  height: 50px;
  font-family: calibri,arial,verdana,sans-serif;
  font-size: 2.5em;
  text-align: center;
  float: left;
  border: 1px #888888 solid;
}
#cal_days_weekend {
  width: 60px;
  height: 50px;
  font-family: calibri,arial,verdana,sans-serif;
  font-size: 2.5em;
  text-align: center;
  background-color: #ffcccc;
  float: left;
  border: 1px #888888 solid;
}
#cal_current {
  color: #000000;
}
.ghosted {
  color: #888888;
}
.cal_day {
  background-color: #ccffcc;
}
.cal_weekday {
  background-color: #ccccff;
}
.cal_weekend {
  background-color: #ffcccc;
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.