rahul s negi
rahul s negi

Reputation: 139

How to show the years b/w to specific dates

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.

Upvotes: 0

Views: 57

Answers (3)

Peter
Peter

Reputation: 9143

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>

Upvotes: 0

devpro
devpro

Reputation: 16117

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

Upvotes: 1

Indrasis Datta
Indrasis Datta

Reputation: 8606

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

Upvotes: 2

Related Questions