Jump to content

Start Date and End Date array


ppunk

Recommended Posts

Hi, I am trying to set up a page where visitors can fill out a form on my site to list their events that are going on. I saw another site that had it setup with a Start Date (month and day drop-downs) and then the length of the event (days drop-down; they had 7 days). So if I wanted to list an event starting tomorrow and have it be 4 days long, it would be like this: Start Date: June 6, 2006, Length: 4 days, so the last day would be June 9, 2006. (6, 7, 8, 9).

After they fill out the form, it would get entered in the database similar to this: startDate = 2006-06-06, endDate = 2006-06-09. I was able to set this up on my own, but there were two flaws I couldn't figure out. First, one extra day was added to the total everytime. Example: I choose 2006-06-01 as start date with a length of 10 days. When it prints out the array it goes from 2006-06-02 to 2006-06-12. It should have been: 01 to 10. I am clueless here.

Another problem I have found is when you choose a later day of the month and a longer length, it doesn't do anything at all. Example: I choose 2006-06-25 with a length of 10 days, it should start at 25 and go to 04 (July). It doesn't do anything. I was playing around with the day and length and found once I get to the 21st day and choose a length of 10, it doesn't work. It takes a long time to load the next page and when it does it is blank. If I go back to my form and choose the 20th with a length of 10, it works right away. I assume it has something to do with the date. There are only 30 days in June, so when I go up one more day (the 21st) and choose 10 day length, it is looking for June 31st, which doesn't exist. What is weird is that if I choose June 20th and length of 10, it prints out from June 21 to July 01 (remember, even though I chose 20 it is printing out 21 for some reason).

I should also mention why I am trying to get the dates in between the Start and End Dates printed to an array. It is because I have a calendar set up and it needs the Month/Day for each day of an event in order for it to highlight the days on the calendar. I will of course have to work on getting the 'Y-m-d' formats from the array to format to 'F' and 'd' for the calendar, but that is later on.

Well, here is my current code.
This is the form:
[code]
<form id="form1" name="form1" method="get" action="form2.php">
Start Date:
  <?php
$curr_month = date("F");
$months = array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
$month_values = array("01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12");  
echo "<select name=\"sMonth\">";
for ($i=0;$i<count($months);$i++) {
    echo "<option value=\"". $month_values[$i]. "\"";
    if ($curr_month==$months[$i]) { echo " selected"; }
    echo ">". $months[$i]. "</option>\n";
}
echo "</select>";
?>
<?php
$curr_day = date("j");
$days = array("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31");
echo "<select name=\"sDay\">";
for ($i=0;$i<count($days);$i++) {
    echo "<option value=\"". $days[$i]. "\"";
    if ($curr_day==$days[$i]) { echo " selected"; }
    echo ">". $days[$i]. "</option>\n";
}
echo "</select>";
?>

Event Length:
  <select name="selDay" id="selDay">
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
    <option value="10">10</option>
  </select>
  Days<br />
  <br />
  <input type="submit" name="Submit" value="Submit" />
</form>
[/code]
And here is the page that processes the form data and array:
[code]
<?php
// Get Start Date and Length from form
$sMonth = $_GET['sMonth'];
$sDay = $_GET['sDay'];
$selDay = $_GET['selDay'];

// Current Year
$currYear = date('Y');

// Format Start Date
$sMD = "-$sMonth-$sDay";
$sYMD = $currYear.$sMD;
$startDate = date('Y-m-d',strtotime($sYMD));

// Create End Date from Start Date + Length
$endDate = date('Y-m-d', strtotime("$startDate+$selDay days"));

// Save Dates in array - Borrowed this from another post on PHP Freaks
$arrDates = array();
for ($i = $startDate; $i <= $endDate; $i++)
$arrDates[] = @date('Y-m-d', strtotime($i . " days"));
  
// Array with 12 date-ready slots, just for testing
echo $arrDates[0]; echo "<br>";
echo $arrDates[1]; echo "<br>";
echo $arrDates[2]; echo "<br>";
echo $arrDates[3]; echo "<br>";
echo $arrDates[4]; echo "<br>";
echo $arrDates[5]; echo "<br>";
echo $arrDates[6]; echo "<br>";
echo $arrDates[7]; echo "<br>";
echo $arrDates[8]; echo "<br>";
echo $arrDates[9]; echo "<br>";
echo $arrDates[10]; echo "<br>";
echo $arrDates[11]; echo "<br>";

?>
[/code]
At this point, if you choose June 5th on the form with a length of 3 days, it prints out like this on the second page:
[code]
2006-06-06
2006-06-07
2006-06-08
2006-06-09
[/code]
I hope I explained everything clear enough.
Once again, any help would be much appreciated!
Thanks!

-Steve
Link to comment
Share on other sites

In you loop to get the endDate from the startDate, you are trying to add 1 to a string in the format 'Y-m-d' which really doesn't work:
[code]<?php
startDate = date('Y-m-d',strtotime($sYMD));

// Create End Date from Start Date + Length
$endDate = date('Y-m-d', strtotime("$startDate+$selDay days"));

// Save Dates in array - Borrowed this from another post on PHP Freaks
$arrDates = array();
for ($i = $startDate; $i <= $endDate; $i++)
$arrDates[] = @date('Y-m-d', strtotime($i . " days"));
?>[/code]
What you should be doing is using the UNIX time stamp and adding the number of seconds in a day (86400):
[code]<?php
$startDate = strtotime($sYMD);
$endDate = startDate + (86400 * $selDays);
for ($i = $startDate; $i<= $endDate;$i += 86400)
     $arrDates[] = date('Y-m-d',$i);
?>[/code]

Ken
Link to comment
Share on other sites

Wow! That was a [b]terrific [/b]reply! You helped me solve all my problems!!
The only thing I had to change from your example (besides two typos) was this:
[code]$endDate = ($startDate - 1) + (86400 * $selDay);[/code]
I did ($startDate - 1) because it was adding one too many days to the total still. By adding that it is now taking one off. So when I choose June 5th and 10 days length, it goes to June 14th. Same with the end of the month days, when I select June 25th now with a length of 10 days, it goes from June 25 to July 04.
Just awesome!

Do you know why it was adding an extra day to the total? Not a big deal, I am so glad it is working now!

Thanks again for the great help!!

-Steve
Link to comment
Share on other sites

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.