2016-03-14 29 views
0

我正在使用php。在我的項目中,當學生註冊時,他選擇他的課程的開始和結束日期,例如(2012/06/05) - (2015/06/25)。現在他每六個月就提交一次費用。現在我想要做的是,當學生填寫表格提交費用時,他只需要選擇那些他將參加課程的年份即2012,2013,2014 and 2015。怎麼做。
所以任何人都可以請出示我。
在此先感謝。如何顯示特定日期的年/年

回答

1

您需要使用strtotime()從開始和結束日期越來越一年,比獲得兩年間所有年份:

$start = date('Y', strtotime('2012/06/05')); 
$end = date('Y', strtotime('2015/06/25')); 

$years = array(); 
for ($end = $start; $end < date('Y'); $end++) { 
    $years[] = $end; 
} 

echo "<pre>"; 
echo implode(",",$years); //2012,2013,2014,2015 
2

試試這個:

$begin = date('Y', strtotime('2012-06-05')); 
$end = date('Y', strtotime('2015-06-25')); 
$years = array(); 

while($begin <= $end){ 
    $years[] = $begin++; 
} 

echo "<pre>"; 
print_r($years); 

它給:

Array 
(
    [0] => 2012 
    [1] => 2013 
    [2] => 2014 
    [3] => 2015 
) 

此$年是一個數組,其中包含所輸入的兩個日期之間的所有年份用戶。

希望這會有所幫助。 和平!的xD

0

這可以很容易的:

$start = new DateTime('2012/01/20'); 
$end = new DateTime('2015/01/20'); 

$years = range($start->format('Y'), $end->format('Y')); 

<select name="year"> 
<?php foreach ($years as $year): ?> 
    <option value="<?= $year; ?>"><?= $year; ?></option> 
<?php endforeach; ?> 
</select>