Jump to content

Date in forms


khristian

Recommended Posts

Hi,

 

I am trying to create a form which has a drop down box for the date, however I am having a few problems. What I want it to do is have a couple of standard options, and then todays date, and something like the next 28 days.

 

I have tried

<?$actiondate = date('j F Y');?>

<form>

<select name="date" size="1">

<option selected>-- SELECT --</option>

<option>To Be Confirmed</option>

<option><?echo $actiondate?></option>

<option><?echo $actiondate+1?></option>

<option><?echo $actiondate+2?></option>

<option><?echo $actiondate+3?></option>

<option><?echo $actiondate+4?></option>

<option><?echo $actiondate+5?></option>

</select>

 

but all I get is:

-- SELECT --

To Be Confirmed

29 February 2008

30

31

32

33

etc etc

 

Any advice anyone?

Link to comment
Share on other sites

your $actiondate variable is a string, you can't just increment it that way. You need to use timestamps thusly...

<select name="date">
    <option value="">--Select Something--</option>
    <option value="TBC">T.B.C</option>
<?php
    $now = time();
    
    for ($i=0;$i<28;$i++) :
        $ts     =  $now + (86400 * $i);
        $date =  date('j F Y',$ts);
?>
        <option value="<?php echo $ts ?>"><?php echo $date ?></option>

<?php endfor; ?>
</select>
    

Also note that your option's need value attributes to be of any use :)

 

In my example I've made that value the raw timestamp so you can easily convert it into whatever format you need when its sent back to the server for processing.

Link to comment
Share on other sites

...or this way using ISO date format for option values

<?php
$today = mktime(0,0,0);

echo "<select name='date'>";
echo "<option selected>-- SELECT --</option><option>To Be Confirmed</option>";

for ($i=0; $i<29; $i++)
{
    $t = strtotime("+$i days", $today);
    $val = date ('Y-m-d', $t);
    $opt = date ('d M Y', $t);
    
    echo "<option value='$val'>$opt</option>";
}
echo '</select>';
?> 

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.