Jump to content

Recommended Posts

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 />

Link to comment
https://forums.phpfreaks.com/topic/165829-solved-in_array-with-date-formats/
Share on other sites

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...

$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.

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);
         }

?>

$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.

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.