I am working on a php. In my project when a student register, he chooses starting and ending date of his course e.g (2012/06/05)
- (2015/06/25)
. Now he submit his fees after every six months. now what I want to do is that when student fill the form to submit his fees he only have to select the years from those years for which he will do the course i.e 2012,2013,2014 and 2015
. How to do that.
So can anyone please show me someway out there.
Thanks in advance.
Asked
Active
Viewed 53 times
0

devpro
- 16,184
- 3
- 27
- 38

rahul s negi
- 139
- 14
3 Answers
2
Try this:
$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);
it gives:
Array
(
[0] => 2012
[1] => 2013
[2] => 2014
[3] => 2015
)
This $years is an array containing all the years between the two dates entered by the user.
Hope this helps. Peace! xD

Indrasis Datta
- 8,692
- 2
- 14
- 32
1
You need to use strtotime()
for getting year from start and end date as and than get all years between two years as:
$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

devpro
- 16,184
- 3
- 27
- 38
0
This can be done very easy:
$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>

Peter
- 8,776
- 6
- 62
- 95