Jump to content

[SOLVED] best way to insert date on a form


sanderphp

Recommended Posts

I have a form where a user fills out the date when the event occurred.  Currently you have to type 2008/5/6 in the box everytime. I would like to simply it. 

 

Either I could probably just have them type in the month/day and concatenate it with the year or I could have the year filled in the already.   Ideally I would have one of this little popup calendars that so many sites have where you click on a date, but that is probably a stretch for me at this point. If you have other suggestions, I'm open to them. I've very new to php.

 

What do you guys suggest?

 

$date = $_POST['date'];
$eventdate = '2008'$date;

 

something like this?  I doubt that code is correct.

 

 

Link to comment
Share on other sites

hey, here is a quick fix for you. i think it should work, if not you can play around with the idea

 


Add:
<select>
<option value="January" <?php if (date('F') == "January"){ echo "selected";} ?>>January</option>
<option value="February" <?php if (date('F') == "February"){ echo "selected";} ?>>February</option>
<option value="March" <?php if (date('F') == "March"){ echo "selected";} ?>>March</option>
</select>


Link to comment
Share on other sites

Ok, been trying to wrap my head around this. Here is how I'm using this code on the page. 

Don't I need something to get the current year, month, day and pass that as the default?

echo date('Y');

 

 

Here is what I have thus far:

 

  <label>dateyear
  <select name="dateyear" id="dateyear">
  
<option value="2008" <?php if (date('Y') == "2008"){ echo "selected";} ?>>2008</option>
<option value="2009" <?php if (date('Y') == "2009"){ echo "selected";} ?>>2009</option>
<option value="2010" <?php if (date('Y') == "2010"){ echo "selected";} ?>>2010</option>
</select>

  </label>
  
  <label>datemonth
  <select name="datemonth" id="datemonth">
  
<option value="January" <?php if (date('F') == "January"){ echo "selected";} ?>>January</option>
<option value="February" <?php if (date('F') == "February"){ echo "selected";} ?>>February</option>
<option value="March" <?php if (date('F') == "March"){ echo "selected";} ?>>March</option>
</select>

  </label>

 

 

 

Link to comment
Share on other sites

What you have here is fine:

 

  <label>dateyear
  <select name="dateyear" id="dateyear">
  
<option value="2008" <?php if (date('Y') == "2008"){ echo "selected";} ?>>2008</option>
<option value="2009" <?php if (date('Y') == "2009"){ echo "selected";} ?>>2009</option>
<option value="2010" <?php if (date('Y') == "2010"){ echo "selected";} ?>>2010</option>
</select>

  </label>
  
  <label>datemonth
  <select name="datemonth" id="datemonth">
  
<option value="January" <?php if (date('F') == "January"){ echo "selected";} ?>>January</option>
<option value="February" <?php if (date('F') == "February"){ echo "selected";} ?>>February</option>
<option value="March" <?php if (date('F') == "March"){ echo "selected";} ?>>March</option>
</select>

  </label>

 

You are 'getting' the current year and month in your example, it works. Just extend the months a little to get this month.....try it and see.

 

<label>dateyear
  <select name="dateyear" id="dateyear">
  
<option value="2008" <?php if (date('Y') == "2008"){ echo "selected";} ?>>2008</option>
<option value="2009" <?php if (date('Y') == "2009"){ echo "selected";} ?>>2009</option>
<option value="2010" <?php if (date('Y') == "2010"){ echo "selected";} ?>>2010</option>
</select>

  </label>
  
  <label>datemonth
  <select name="datemonth" id="datemonth">
  
<option value="January" <?php if (date('M') == "January"){ echo "selected";} ?>>January</option>
<option value="February" <?php if (date('M') == "February"){ echo "selected";} ?>>February</option>
<option value="March" <?php if (date('M') == "March"){ echo "selected";} ?>>March</option>
<option value="April" <?php if (date('M') == "April"){ echo "selected";} ?>>April</option>
<option value="May" <?php if (date('M') == "May"){ echo "selected";} ?>>May</option>
<option value="June" <?php if (date('M') == "June"){ echo "selected";} ?>>June</option>
</select>

 

  </label>

 

Link to comment
Share on other sites

Is there a better way to list the days of the month? This seems like it will take a while to type in ...

 

<option value="1" <?php if (date('Y') == "1"){ echo "selected";} ?>>1</option>
<option value="2" <?php if (date('Y') == "2"){ echo "selected";} ?>>2</option>
<option value="3" <?php if (date('Y') == "3"){ echo "selected";} ?>>3</option>

Link to comment
Share on other sites

Is there a better way to list the days of the month? This seems like it will take a while to type in ...

 

How about....

 

         <select name="date">
       <?php
          $date1 = date("j");
          echo "<option value=\"$date1\" selected=\"selected\">$date1</option>";
          $date = 1; 
          while ( $date <=31 ) 
          { 
          echo "<option value=\"$date\">$date</option>"; 
          $date++; 
          } 
         ?>
          </select>

Link to comment
Share on other sites

How about....

 

         <select name="date">
       <?php
          $date1 = date("j");
          echo "<option value=\"$date1\" selected=\"selected\">$date1</option>";
          $date = 1; 
          while ( $date <=31 ) 
          { 
          echo "<option value=\"$date\">$date</option>"; 
          $date++; 
          } 
         ?>
          </select>

Hmm, I think you'd get two options with the value of the current day then. This could also work if you only want it to show once:

<?php
for ($x=1; $x<=31; $x++)
{
if (date('j') != $x)
{
	echo "<option value=\"$x\">$x</option>";
}
else
{
	echo "<option value=\"$x\" selected=\"selected\">$x</option>";
}

}
?>

Link to comment
Share on other sites

or to shorten things up a bit (also, please indulge me in why the code formatting is automatically removing my backslashes around selected=\"selected\" upon posting):

 

<?php 
$day = date('j');
for ($x=1; $x<=31; $x++) { 
    echo "<option value=\"$x\" . (($day != $x) ? " selected=\"selected\"" : "") . ">$x</option>";
}
?>

Link to comment
Share on other sites

Here is what I added but I'm getting a Parse error: syntax error, unexpected T_STRING, expecting ',' or ';' in: 

 

 

<label> Day 2
  <select name="dateday2" id="dateday2">
  <?php 
$day = date('j');
for ($x=1; $x<=31; $x++) { 
    echo "<option value=\"$x\" . (($day != $x) ? " selected="\selected\"" : "") . ">$x</option>";
}
?>

</label>  

Link to comment
Share on other sites

echo "<option value=\"$x\" . (($day != $x) ? " selected="\selected\"" : "") . ">$x</option>";

Hmm, there is no ending quote after your escaped quote... And after you placed the backslash before the quote after selected. So something like below should work. Also, I think it should be the equal operator instead of not equal.

 

echo "<option value=\"$x\"" . (($day == $x) ? " selected=\"selected\"" : "") . ">$x</option>";

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.