Yesideez Posted April 7, 2009 Share Posted April 7, 2009 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 https://forums.phpfreaks.com/topic/153012-my-first-calendar-can-it-be-optimised/ Share on other sites More sharing options...
revraz Posted April 7, 2009 Share Posted April 7, 2009 I think you are doing a lot of unnecessary code and work. Look at this example http://php.about.com/od/finishedphp1/ss/php_calendar.htm Link to comment https://forums.phpfreaks.com/topic/153012-my-first-calendar-can-it-be-optimised/#findComment-803649 Share on other sites More sharing options...
Yesideez Posted April 7, 2009 Author Share Posted April 7, 2009 Thanks but I don't want to use tables. Link to comment https://forums.phpfreaks.com/topic/153012-my-first-calendar-can-it-be-optimised/#findComment-803690 Share on other sites More sharing options...
Axeia Posted April 7, 2009 Share Posted April 7, 2009 Any reason why not? I'd say a calendar qualifies as tabular data. You might want to use strftime to get the day / monthnames to make it more portable language wise. Link to comment https://forums.phpfreaks.com/topic/153012-my-first-calendar-can-it-be-optimised/#findComment-803694 Share on other sites More sharing options...
revraz Posted April 7, 2009 Share Posted April 7, 2009 Regardless if you use tables or not, look at their logic to load in the days and months. Link to comment https://forums.phpfreaks.com/topic/153012-my-first-calendar-can-it-be-optimised/#findComment-803715 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.