r00ttap Posted July 13, 2009 Share Posted July 13, 2009 Is there something I should know about in_array and the way it handles date formats? Essentially I'm setting up form validation with a calendar and I want to create a sticky form so if you selected a date but forgot something else it will remember what you selected. I started out with code like this: $day_counter is being pulled from a loop that counts the days in the month and will return 01 - 31, etc. <?php $fullDate = date("Y-m-$day_counter"); $selectedDates = array(); if(in_array($fullDate, $selectedDates)) { $checked = "checked"; } ?> <input type="checkbox" name="selectedDates[]" value="$fullDate" $checked /> Quote Link to comment https://forums.phpfreaks.com/topic/165829-solved-in_array-with-date-formats/ Share on other sites More sharing options...
.josh Posted July 13, 2009 Share Posted July 13, 2009 in_array does a string comparison of the value vs. all the values in the array, so as long as it's formatted the same, no, there's nothing special you need to know. But I'm looking at your code and I see that you have initialized $selectedDates as an array but I don't see anything in there as far as you having any elements in that array, so you're just comparing $fullDate to an empty array... Quote Link to comment https://forums.phpfreaks.com/topic/165829-solved-in_array-with-date-formats/#findComment-874682 Share on other sites More sharing options...
r00ttap Posted July 13, 2009 Author Share Posted July 13, 2009 $selectedDates are being pulled from the form. There is a checkbox for each day of the month. I didn't want to post the whole calendar function because it's quite a bit of code. Everything I posted is being done within the for loop. I forgot to mention that once I pulled just the day from the $selectedDates and used that to match against selected days it worked but since I'm adding another month I need a full date format. Quote Link to comment https://forums.phpfreaks.com/topic/165829-solved-in_array-with-date-formats/#findComment-874687 Share on other sites More sharing options...
r00ttap Posted July 13, 2009 Author Share Posted July 13, 2009 Here is the full function if anyone has the time to read through it. The problem lies with these two lines: if($post) { if(in_array($fullDate1, $cancelDate)) { $checked = "checked"; } } and if($post) { if(in_array($fullDate2, $cancelDate)) { $checked = "checked"; } } <?php function showCalendar1($date) { global $cancelDate; //If no parameter is passed use the current date. if($date == null) $date = getDate(); $day = $date["mday"]; $month = $date["mon"]; $month_name = $date["month"]; $year = $date["year"]; $dateSelect = mysql_query("SELECT date FROM course_cancellations_dates WHERE id = '$_GET[id]'") or die(mysql_error()); $cancelDates = array(); while($r=mysql_fetch_assoc($dateSelect)) { $cancelDates[] = date("d", strtotime($r['date'])); } $this_month = getDate(mktime(0, 0, 0, $month, 1, $year)); $next_month = getDate(mktime(0, 0, 0, $month + 1, 1, $year)); //Find out when this month starts and ends. $first_week_day = $this_month["wday"]; $days_in_this_month = round(($next_month[0] - $this_month[0]) / (60 * 60 * 24)); $calendar_html = "<table style=\"width: 360px; height: 100%; border: 1px solid #CCC; border-collapse: collapse\">"; $calendar_html .= "<tr><td colspan=\"7\" align=\"center\" style=\"background: #EEEEEE\">" . $month_name . " " . $year . "</td></tr>"; $calendar_html .= "<tr><td align=\"center\" style=\"border: 1px solid #CCC; height: 30px; width: 60px;\">Sun</td> <td align=\"center\" style=\"border: 1px solid #CCC; height: 30px; width: 60px;\">Mon</td> <td align=\"center\" style=\"border: 1px solid #CCC; height: 30px; width: 60px;\">Tue</td> <td align=\"center\" style=\"border: 1px solid #CCC; height: 30px; width: 60px;\">Wed</td> <td align=\"center\" style=\"border: 1px solid #CCC; height: 30px; width: 60px;\">Thu</td> <td align=\"center\" style=\"border: 1px solid #CCC; height: 30px; width: 60px;\">Fri</td> <td align=\"center\" style=\"border: 1px solid #CCC; height: 30px; width: 60px;\">Sat</td></tr><tr>"; //Fill the first week of the month with the appropriate number of blanks. for($week_day = 0; $week_day < $first_week_day; $week_day++) { $calendar_html .= "<td style=\"border: 1px solid #CCC;\"> </td>"; } $week_day = $first_week_day; for($day_counter = 1; $day_counter <= $days_in_this_month; $day_counter++) { $week_day %= 7; if($week_day == 0) { $calendar_html .= "</tr><tr>"; } //Do something different for the current day. if($day == $day_counter) { $checked = ""; $fullDate1 = date("Y-m-$day_counter"); if(in_array($day_counter, $cancelDates)) { $checked = "checked"; } if($post) { if(in_array($fullDate1, $cancelDate)) { $checked = "checked"; } } $calendar_html .= "<td valign=\"top\" align=\"right\" style=\"background: #BFEFFF\"><b>" . $day_counter . " <br /><center><input type=\"checkbox\" name=\"cancelDate[]\" value=\"$fullDate1\" $checked /></center></b></td>"; } else { $checked = ""; $fullDate2 = date("Y-m-$day_counter"); if(in_array($day_counter, $cancelDates)) { $checked = "checked"; } if($post && in_array($fullDate2, $cancelDate)) { $checked = "checked"; } $calendar_html .= "<td valign=\"top\" align=\"right\" style=\"border: 1px solid #CCC\">" . $day_counter . " <br /><center><input type=\"checkbox\" name=\"cancelDate[]\" value=\"$fullDate2\" $checked /></center> </td>"; } $week_day++; } $calendar_html .= "</tr>"; $calendar_html .= "</table>"; return($calendar_html); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/165829-solved-in_array-with-date-formats/#findComment-874689 Share on other sites More sharing options...
.josh Posted July 13, 2009 Share Posted July 13, 2009 $cancelDates[] = date("d", strtotime($r['date'])); You did not update that to reflect the year/month. Also, "d" returns the day with a leading zero, whereas $day_counter will not have a leading zero. You will either want to use "j" which is day without leading zero, or pad $day_counter. Quote Link to comment https://forums.phpfreaks.com/topic/165829-solved-in_array-with-date-formats/#findComment-874699 Share on other sites More sharing options...
r00ttap Posted July 13, 2009 Author Share Posted July 13, 2009 Wow... you are absolutely right! Also, I've realized the if statement is throwing it off. I changed $post to isset($date) and all seems to be well. Thanks a lot! Quote Link to comment https://forums.phpfreaks.com/topic/165829-solved-in_array-with-date-formats/#findComment-874709 Share on other sites More sharing options...
.josh Posted July 13, 2009 Share Posted July 13, 2009 I also thought I'd mention that it looks like from your script you have register_globals turned on and are using them. That is not very secure, and it's also deprecated. So for instance, you should be using $_POST instead of $post and if you have for instance a form field named 'something' you should be referring to it as $_POST['something'] instead of $something. Quote Link to comment https://forums.phpfreaks.com/topic/165829-solved-in_array-with-date-formats/#findComment-874726 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.