Reputation: 47
I have build a select date dropdown list, containing dates for a period of time, starting from the current day. I would like to be able to submit the selected date in a database, however I am having problems with creating the option value.
<?php
$begin = new DateTime('today');
$end = new DateTime('today+120day');
$interval = DateInterval::createFromDateString('1 day');
$period = new DatePeriod($begin, $interval, $end);
<select>
foreach ($period as $dt)
echo "<option value='[]'>".$dt-> format(" d.m.Y")."</option>";
</select>
?>
Upvotes: 0
Views: 204
Reputation: 500
You can also use this
<?php
$begin = new DateTime('today');
$end = new DateTime('today+120day');
$interval = DateInterval::createFromDateString('1 day');
$period = new DatePeriod($begin, $interval, $end);
echo "<select>";
foreach ($period as $dt)
echo "<option value='[]'>".$dt-> format(" d.m.Y")."</option>";
echo "</select>";
?>
Upvotes: 1
Reputation: 2957
Use this:
<?php
$begin = new DateTime('today');
$end = new DateTime('today+120day');
$interval = DateInterval::createFromDateString('1 day');
$period = new DatePeriod($begin, $interval, $end);
?>
<select>
<?php
foreach ($period as $dt)
{
echo "<option value='". $dt->format("d.m.y"). "'>". $dt->format(" d.m.Y")."</option>";
}
?>
</select>
Upvotes: 1