jrws Posted January 26, 2009 Share Posted January 26, 2009 Hey all, But your surprised that its not a problem XD. Well anyway, I have just completed a tutorial about a simple calendar, and I was wondering how would I make it highlight the current day? I was thinking using span tags, and an if, but it didn't work. (Crashed firefox XD) So here's the code, mind you I didn't edit anything 'cept for the center tag in the table. <?php $date = time(); $day = date('d', $date); $month = date('m', $date); $year = date('y', $date); $first_day = mktime(0, 0, 0, $month, 1, $year); $title = date('F', $first_day); $day_of_week = date('D', $first_day); switch ($day_of_week) { case 'Sun': $blank = 0; break; case 'Mon': $blank = 1; break; case 'Tus': $blank = 2; break; case 'Wed': $blank = 3; break; case 'Thu': $blank = 4; break; case 'Fri': $blank = 5; break; case 'Sat': $blank = 6; break; } $days_in_month = cal_days_in_month(0, $month, $year); echo '<table border = "6" width = "394">'; echo '<tr><th colspan ="60">' . $title . ' ' . $year . '</th></tr>'; echo '<tr><td width = "62">Su</td><td width = "62">M</td><td width = "62">Tu</td><td width = "62">W</td><td width = "62">Th</td><td width = "62">F</td><td width = "62">Sa</td></tr>'; $day_count = 1; echo '<tr>'; while ($blank > 0) { echo '<td></td>'; $blank -= 1; $day_count++; } $day_num = 1; while ($day_num <= $days_in_month) { echo '<td style="text-align:center">' . $day_num . '</td>'; $day_num++; $day_count++; if ($day_count > 7) { echo '</tr><tr>'; $day_count = 1; } } while ($day_count > 1 && $day_count <= 7) { echo '<td></td>'; $day_count++; } echo '</tr></table>'; ?> So any help with this is much appreciated! EDIT: Sorry just realized this would be classified as a third party script, so could a mod move it please? Link to comment https://forums.phpfreaks.com/topic/142444-solved-not-really-a-problem-but-rather-a-question/ Share on other sites More sharing options...
Psycho Posted January 26, 2009 Share Posted January 26, 2009 The code below sets a style for the current day. Also reduced the amount of code needed and corrected some minor issues (e.g. creating a 'blank' row if the month ends on a saturday). <?php $date = time(); $day = date('d', $date); $month = date('m', $date); $year = date('y', $date); $title = date('F', $date); $first_day = mktime(0, 0, 0, $month, 1, $year); $day_of_week = date('N', $first_day); $days_in_month = cal_days_in_month(0, $month, $year); //Counter for the weekday position $day_count = 1; //Start the table echo "<table border=\"6\" width=\"394\">\n"; echo "<tr><th colspan=\"7\">{$title} {$year}</th></tr>\n"; echo "<tr><th width=\"62\">Su</th><th width=\"62\">M</th><th width=\"62\">Tu</th><th width=\"62\">W</th><th width=\"62\">Th</th><th width=\"62\">F</th><th width=\"62\">Sa</th></tr>\n"; echo "<tr>\n"; //Create empty days as needed for beginning of month while ($day_count <= $day_of_week) { echo "<td></td>\n"; $blank -= 1; $day_count++; } //Create the days of the month for ($day_num=1; $day_num<=$days_in_month; $day_num++) { //Open row if first day of week if ($day_count==1) { echo "<tr>\n"; } $style = ($day_num==$day) ? ' style="background-color:#00ff00;"' : ''; echo "<td style=\"text-align:center\"{$style}>{$day_num}</td>\n"; $day_count++; //Close row if last day of week if ($day_count > 7) { echo "</tr>\n"; $day_count = 1; } } //Create empty days as needed for end of month if ($day_count>1) { for($day_count; $day_count<=7; $day_count++) { echo "<td></td>\n"; } echo "</tr>\n"; } //Close the table echo "</table>\n"; ?> Link to comment https://forums.phpfreaks.com/topic/142444-solved-not-really-a-problem-but-rather-a-question/#findComment-746375 Share on other sites More sharing options...
jrws Posted January 26, 2009 Author Share Posted January 26, 2009 Thanks mate, James Link to comment https://forums.phpfreaks.com/topic/142444-solved-not-really-a-problem-but-rather-a-question/#findComment-746418 Share on other sites More sharing options...
Psycho Posted January 26, 2009 Share Posted January 26, 2009 Had some minor errors that I found. Here is updated code <?php $date = time(); $month = date('m', $date) + 2; $year = date('y', $date); $title = date('F', $date); $days_in_month = cal_days_in_month(0, $month, $year); //Start the table echo "<table border=\"6\" width=\"394\">\n"; echo " <tr><th colspan=\"7\">{$title} {$year}</th></tr>\n"; echo " <tr>\n"; echo " <th width=\"62\">Su</th><th width=\"62\">M</th><th width=\"62\">Tu</th>\n"; echo " <th width=\"62\">W</th><th width=\"62\">Th</th><th width=\"62\">F</th><th width=\"62\">Sa</th>\n"; echo " </tr>\n"; //Create empty days as needed for beginning of month for ($empty=0; $empty<(date('N', mktime(0, 0, 0, $month, 1, $year))%7); $empty++) { if ($empty == 0) { echo "<tr>\n"; } echo " <td></td>\n"; } //Create the days of the month for ($day=1; $day<=$days_in_month; $day++) { $dow = date('N', mktime(0, 0, 0, $month, $day, $year)) % 7; //Open row if first day of week if ($dow == 0) { echo " <tr>\n"; } $style = ($day==date('d', $date)) ? ' style="background-color:#00ff00;"' : ''; echo " <td style=\"text-align:center\"{$style}>{$day}</td>\n"; //Close row if last day of week if ($dow == 6) { echo " </tr>\n"; } } //Create empty days as needed for end of month for ($dow; $dow<6; $dow++) { echo " <td></td>\n"; if ($dow == 5) { echo " </tr>\n"; } } //Close the table echo "</table>\n"; ?> Link to comment https://forums.phpfreaks.com/topic/142444-solved-not-really-a-problem-but-rather-a-question/#findComment-746796 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.