however Posted February 16, 2013 Share Posted February 16, 2013 Hello, this is my first post in this new forum. I really hope that someone can help me, as I am really getting headaches from sleepless nights trying to solve this problem. I need to say in advance that I am a total newbie with scripting in php. I have found this php calendar script but i would like it to have today's date of a different color. Any help/suggestion would be gratefully appreciated. calendar.php <html> <body> <?php $month_Names = Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"); if (!isset($_REQUEST["month"])) $_REQUEST["month"] = date("n"); if (!isset($_REQUEST["year"])) $_REQUEST["year"] = date("Y"); $Current_Month = $_REQUEST["month"]; $Current_Year = $_REQUEST["year"]; $prev_year = $Current_Year; $next_year = $Current_Year; $prev_month = $Current_Month-1; $next_month = $Current_Month+1; if ($prev_month == 0 ) { $prev_month = 12; $prev_year = $Current_Year - 1; } if ($next_month == 13 ) { $next_month = 1; $next_year = $Current_Year + 1; } ?> <table width="200"> <tr align="center"> <td style="color:#FFFFFF"> <table width="100%" border="0" cellspacing="2" cellpadding="0"> <tr> <td align="left"> <a href="<?php echo $_SERVER["PHP_SELF"] . "?month=". $prev_month . "&year=" . $prev_year; ?>" style="color:#808080"> << </a></td> <td align="center" style="color:#000000"><strong><?php echo $month_Names[$Current_Month-1].' '.$Current_Year; ?></strong></td> <td align="right"><a href="<?php echo $_SERVER["PHP_SELF"] . "?month=". $next_month . "&year=" . $next_year; ?>" style="color:#808080"> >> </a></td> </tr> </table> </td> </tr> <tr> <td align="center" clspan="7"> <table width="100%" border="0" cellpadding="2" cellspacing="2"> <tr> <td align="center" style="color:#FF0000"><strong>S</strong></td> <td align="center" style="color:#000000"><strong>M</strong></td> <td align="center" style="color:#000000"><strong>T</strong></td> <td align="center" style="color:#000000"><strong>W</strong></td> <td align="center" style="color:#000000"><strong>T</strong></td> <td align="center" style="color:#000000"><strong>F</strong></td> <td align="center" style="color:#FF0000"><strong>S</strong></td> </tr> <?php $timestamp = mktime(0,0,0,$Current_Month,1,$Current_Year); $maxday = date("t",$timestamp); $thismonth = getdate ($timestamp); $startday = $thismonth['wday']; for ($i=0; $i<($maxday+$startday); $i++) { if(($i % 7) == 0 ) echo "<tr>"; if($i < $startday) echo "<td></td>"; else echo "<td align='center' valign='middle' height='20px'>". ($i - $startday + 1) . "</td>"; if(($i % 7) == 6 ) echo "</tr>"; } ?> </table> </td> </tr> </table> </body> </html> How do I change the color of today's date ? Regards, Quote Link to comment https://forums.phpfreaks.com/topic/274578-php-calendar-change-todays-color/ Share on other sites More sharing options...
denno020 Posted February 17, 2013 Share Posted February 17, 2013 Add this into your script //Outside of for loop $today = date("d"); //Today's date (number e.e. 17) $month = date("F"); //This month (full name e.g. February) $year = date("Y"); //This year (4 digits, e.g. 2013) //Inside for loop else if(($i-$startday+1) == $today && $thismonth['month'] == $month && $thismonth['year'] == $year) echo "<td align='center' class='today' valign='middle' height='20px'>". ($i - $startday + 1) . "</td>"; That will add a CSS class to the day that matches today. So in your style sheet, define a background colour for the class '.today' Quote Link to comment https://forums.phpfreaks.com/topic/274578-php-calendar-change-todays-color/#findComment-1412851 Share on other sites More sharing options...
however Posted February 17, 2013 Author Share Posted February 17, 2013 Hi Denno020, thank you very much for your input. I just don't seem to be able to do this. I don't know where to insert the script! Add this into your script //Outside of for loop $today = date("d"); //Today's date (number e.e. 17) $month = date("F"); //This month (full name e.g. February) $year = date("Y"); //This year (4 digits, e.g. 2013) //Inside for loop else if(($i-$startday+1) == $today && $thismonth['month'] == $month && $thismonth['year'] == $year) echo "<td align='center' class='today' valign='middle' height='20px'>". ($i - $startday + 1) . "</td>"; That will add a CSS class to the day that matches today. So in your style sheet, define a background colour for the class '.today' The first part says //Outside of the for loop// and I guess it should be here somewhere in between this lines: <?php $timestamp = mktime(0,0,0,$Current_Month,1,$Current_Year); $maxday = date("t",$timestamp); $thismonth = getdate ($timestamp); $startday = $thismonth['wday']; and the second part of your scrip says, //Inside the for loop//, and I guess it should go somewhere here: for ($i=0; $i<($maxday+$startday); $i++) { if(($i % 7) == 0 ) echo "<tr>"; if($i < $startday) echo "<td></td>"; else echo "<td align='center' valign='middle' height='20px'>". ($i - $startday + 1) . "</td>"; if(($i % 7) == 6 ) echo "</tr>"; } ?> I have tried everywhere but no luck. I have also created a .today class in my style sheet under the calendar table wrapper and still no luck. This is the CSS for the table wrapper: /* Calendar */ #calendar_wrap { padding: 0 15px; text-align: center; } #calendar_wrap table { width: 100%; } #calendar_wrap td { color: #3E3E3E; left: 0px; } .today { background: #DCDCDC; } This may make you laugh in despair, but as I said earlier, i'm really not a pro. Would you help me a little more, pls? Regards, Quote Link to comment https://forums.phpfreaks.com/topic/274578-php-calendar-change-todays-color/#findComment-1412928 Share on other sites More sharing options...
denno020 Posted February 17, 2013 Share Posted February 17, 2013 (edited) I've added it to line 4 below. 1 for ($i=0; $i<($maxday+$startday); $i++) { 2 if(($i % 7) == 0 ) echo "<tr>"; 3 if($i < $startday) echo "<td></td>"; 4 else if(($i-$startday+1) == $today && $thismonth['month'] == $month && $thismonth['year'] == $year) echo "<td align='center' class='today' valign='middle' height='20px'>". ($i - $startday + 1) . "</td>"; 5 else echo "<td align='center' valign='middle' height='20px'>". ($i - $startday + 1) . "</td>"; 6 if(($i % 7) == 6 ) echo "</tr>"; 7 } The place where you added the first 3 lines should be fine. Just so long as your CSS is linked correctly, that will work. If you want to try it without having to rely on the CSS, using this line instead: else if(($i-$startday+1) == $today && $thismonth['month'] == $month && $thismonth['year'] == $year) echo "<td align='center' style="background-color: red;" valign='middle' height='20px'>". ($i - $startday + 1) . "</td>"; It was working fine for me, so hopefully that works for your now also. Denno Edited February 17, 2013 by denno020 Quote Link to comment https://forums.phpfreaks.com/topic/274578-php-calendar-change-todays-color/#findComment-1413018 Share on other sites More sharing options...
however Posted February 27, 2013 Author Share Posted February 27, 2013 Hi Denno, apologies for the long wait in replying to your help suggestion. Unfortunately, it doesn't seem to work for me; I have tried both (with and without CSS styiling) and the result is the same: the entirre page background changes to dark grey overwriting everything else. I will keep trying other options, but if you have any further suggestions I will be glad to try. Regards Quote Link to comment https://forums.phpfreaks.com/topic/274578-php-calendar-change-todays-color/#findComment-1415348 Share on other sites More sharing options...
denno020 Posted February 27, 2013 Share Posted February 27, 2013 Copy your code here and I'll see if I can figure out what's breaking it. Quote Link to comment https://forums.phpfreaks.com/topic/274578-php-calendar-change-todays-color/#findComment-1415350 Share on other sites More sharing options...
however Posted February 27, 2013 Author Share Posted February 27, 2013 Hi again Denno and thank you very much for your input. Below, I have attached the three files, containing the code for the calendar.php, default.css and index.php. Not sure if you would have preferred the actual code copied&pasted here but I thought it would be less messy. Look forward to hearing from you. Regards, index.php calendar.php default.css Quote Link to comment https://forums.phpfreaks.com/topic/274578-php-calendar-change-todays-color/#findComment-1415456 Share on other sites More sharing options...
jcbones Posted February 27, 2013 Share Posted February 27, 2013 People don't like to download files here. You will get a faster response by posting the code inside of [ code ] ... [ /code ] blocks. Quote Link to comment https://forums.phpfreaks.com/topic/274578-php-calendar-change-todays-color/#findComment-1415477 Share on other sites More sharing options...
jcbones Posted February 27, 2013 Share Posted February 27, 2013 This code works on my machine. <html> <body> <?php $month_Names = Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"); if (!isset($_REQUEST["month"])) $_REQUEST["month"] = date("n"); if (!isset($_REQUEST["year"])) $_REQUEST["year"] = date("Y"); $Current_Month = $_REQUEST["month"]; $Current_Year = $_REQUEST["year"]; $prev_year = $Current_Year; $next_year = $Current_Year; $prev_month = $Current_Month-1; $next_month = $Current_Month+1; if ($prev_month == 0 ) { $prev_month = 12; $prev_year = $Current_Year - 1; } if ($next_month == 13 ) { $next_month = 1; $next_year = $Current_Year + 1; } $today = date("d"); //Today's date (number e.e. 17) $month = date("F"); //This month (full name e.g. February) $year = date("Y"); //This year (4 digits, e.g. 2013) ?> <table width="200"> <tr align="center"> <td style="color:#FFFFFF"> <table width="100%" border="0" cellspacing="2" cellpadding="0"> <tr> <td align="left"> <a href="<?php echo $_SERVER["PHP_SELF"] . "?month=". $prev_month . "&year=" . $prev_year; ?>" style="color:#808080"> << </a></td> <td align="center" style="color:#000000"><strong><?php echo $month_Names[$Current_Month-1].' '.$Current_Year; ?></strong></td> <td align="right"><a href="<?php echo $_SERVER["PHP_SELF"] . "?month=". $next_month . "&year=" . $next_year; ?>" style="color:#808080"> >> </a></td> </tr> </table> </td> </tr> <tr> <td align="center" clspan="7"> <table width="100%" border="0" cellpadding="2" cellspacing="2"> <tr> <td align="center" style="color:#FF0000"><strong>S</strong></td> <td align="center" style="color:#000000"><strong>M</strong></td> <td align="center" style="color:#000000"><strong>T</strong></td> <td align="center" style="color:#000000"><strong>W</strong></td> <td align="center" style="color:#000000"><strong>T</strong></td> <td align="center" style="color:#000000"><strong>F</strong></td> <td align="center" style="color:#FF0000"><strong>S</strong></td> </tr> <?php $timestamp = mktime(0,0,0,$Current_Month,1,$Current_Year); $maxday = date("t",$timestamp); $thismonth = getdate ($timestamp); $startday = $thismonth['wday']; for ($i=0; $i<($maxday+$startday); $i++) { if(($i % 7) == 0 ) echo "<tr>"; if($i < $startday) echo "<td></td>"; else if(($i-$startday+1) == $today && $thismonth['month'] == $month && $thismonth['year'] == $year) echo "<td align='center' style=\"background-color: red;\" valign='middle' height='20px'>". ($i - $startday + 1) . "</td>"; else echo "<td align='center' valign='middle' height='20px'>". ($i - $startday + 1) . "</td>"; if(($i % 7) == 6 ) echo "</tr>"; } ?> </table> </td> </tr> </table> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/274578-php-calendar-change-todays-color/#findComment-1415479 Share on other sites More sharing options...
however Posted February 27, 2013 Author Share Posted February 27, 2013 hi jcbones, and thank you so much! Your code works. Thanks also to everyone else for their input. Keep up the good work. Regards, Quote Link to comment https://forums.phpfreaks.com/topic/274578-php-calendar-change-todays-color/#findComment-1415482 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.