简体   繁体   中英

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 eg (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 ie 2012,2013,2014 and 2015 . How to do that.
So can anyone please show me someway out there.
Thanks in advance.

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

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

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM