Dragen Posted April 26, 2007 Share Posted April 26, 2007 Hi, I've got my script which writes a calendar.. It works absolutely fine, but I'm wanting to verify dates from a databse with it. The problem is the dates in my database are in date format: yyyy-mm-dd and the dates written by my calendar are slightly different. It keeps the four digit year, but the calculations make it months and days without a leading zero.. so if the date is the 5th of april 2007 it would be: 2007-4-5 instead of: 2007-04-05 To validate the dates I need the format to be in two digit format. If anyone has the time to look at my code, code you polease tell me how I can change it to two digits.. if possible..? <table border="1" cellpadding="0" cellspacing="0" width="100%" id="calendar"> <tr> <th width="16%" height="48" align="center" valign="bottom"><?php echo $year; ?></th> <th align="center" valign="middle">Sunday</th> <th align="center" valign="middle">Monday</th> <th align="center" valign="middle">Tuesday</th> <th align="center" valign="middle">Wednesday</th> <th align="center" valign="middle">Thursday</th> <th align="center" valign="middle">Friday</th> <th align="center" valign="middle">Saturday</th> </tr> <?php //check if $month is above 0 but below 13. If so it writes out a month, and increments $month by one. //once it reaches 12 it stops. while($month > 0 && $month < 13) { showMonth($month, $year); $month++; } ?> </table> <?php } //This function writes the months function showMonth($month, $year) { $date = mktime(12, 0, 0, $month, 1, $year); $daysInMonth = date('t', $date); // calculate the position of the first day in the calendar (sunday = 1st column, etc) $offset = date("w", $date); $rows = 1; echo "\t\t<tr>\n"; //displays month echo "\t\t\t<td align=\"right\"><strong>". date('F', $date) . "</strong></td>\n"; for($i = 1; $i <= $offset; $i++) { echo "\t\t\t<td width=\"12%\"></td>\n"; } for($day = 1; $day <= $daysInMonth; $day++) { if( ($day + $offset - 1) % 7 == 0 && $day != 1) { echo "\t\t</tr>\n"; echo "\t\t<tr>\n"; echo "\t\t\t<td></td>\n"; $rows++; } //checks if date is booked. If so sets it to class="booked" echo "\t\t\t<td align=\"center\" valign=\"middle\" width=\"12%\" height=\"48\""; //gets data from table to verify date $caldate = $year."-".$month."-".$day; $sql = "SELECT 'date' FROM booked"; if ($result = mysql_query($sql)) { if (mysql_num_rows($result)) { while ($row = mysql_fetch_array($result)) { if( $caldate == $row['date'] ) { echo " class=\"booked\""; } } } else { echo ">not found</td>\n"; exit; } } else { echo ">".mysql_error()."</td>\n"; exit; } echo ">" . $day . "<br />" . $caldate . "</td>\n"; } while( ($day + $offset) <= $rows * 7) { echo "\t\t\t<td></td>\n"; $day++; } echo "\t\t</tr>\n"; } ?> For months I've tried setting $month as 01 instead of 1 and that works until it gets to the second month, then it goes back to just the one digit... Any help would be apprecieted. Thanks Quote Link to comment Share on other sites More sharing options...
taith Posted April 26, 2007 Share Posted April 26, 2007 wonderful invention by the function name of strpad()... should allow you to fluff them up abit ;-) http://us2.php.net/manual/en/function.str-pad.php Quote Link to comment Share on other sites More sharing options...
Dragen Posted April 26, 2007 Author Share Posted April 26, 2007 thanks. that's perfect! Quote Link to comment 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.