1

I have a dropdown that I want to populate with a date starting from last year going up to 5 years in the future but I want it to update automagically every year with php.

Probably easier if I show what I want to end up with than trying to explain.

This Years dropdown should be

<select>
     <option>2012-08-31</option>
     <option>2013-08-31</option>
     <option>2014-08-31</option>
     <option>2015-08-31</option>
</select>

And nextYears dropdown should be

<select>
     <option>2013-08-31</option>
     <option>2014-08-31</option>
     <option>2015-08-31</option>
     <option>2016-08-31</option>
</select>

and so on.

Anybody got any ideas?

4

4 回答 4

1

要添加一年,您可以使用

echo date('Y-m-d H:i:s', strtotime('+1 year'));

增加许多年

for($i=1;$i<=5;$i++)
{
echo date('Y-m-d H:i:s', strtotime('+'.$i.' year'));
}

与选择框一起使用

<select name="years">
for($i=1;$i<=5;$i++)
    {
    ?>
    <option value="<?php echo date('Y-m-d H:i:s', strtotime('+'.$i.' year')); ?>"><?php echo date('Y-m-d H:i:s', strtotime('+'.$i.' year')); ?></option>
   <?php
    }
    ?>
</select>
于 2013-03-21T10:26:00.143 回答
1

增加年限?

for($i=date('Y'); $i<date('Y')+5; $i++){
    echo '<option>'.$i.date('-m-d').'</option>';
}

但最好玩mktime

于 2013-03-21T10:24:31.163 回答
1

你可以做一个这样的功能

function getNextYears($fromDate, $nbYears)
{
    $time = strtotime($fromDate);
    $dates = array();

    for ($i=0; $i<$nbYears; $i++) {
        $dates[] = date('Y-m-d', mktime(0, 0, 0, date('m', $time), date('d', $time), date('Y', $time)+$i));        
    }

    return $dates;
}
于 2013-03-21T10:28:50.067 回答
0

像这样的东西可能会帮助你

<?php

$start_year = date('Y')-1;
$end_year   = $start_year+5;

for($i=$start_year; $i<=$end_year; $i++)
{
   echo '<option>'.$i.date('-m-d').'</option>';
}

?>

于 2013-03-21T10:31:57.647 回答