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
https://forums.phpfreaks.com/topic/93777-date-in-forms/
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
https://forums.phpfreaks.com/topic/93777-date-in-forms/#findComment-480543
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
https://forums.phpfreaks.com/topic/93777-date-in-forms/#findComment-480552
Share on other sites

Archived

This topic is now archived and is closed to further replies.

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