简体   繁体   English

如何显示特定日期的黑白

[英]How to show the years b/w to specific dates

I am working on a php. 我正在使用PHP。 In my project when a student register, he chooses starting and ending date of his course eg (2012/06/05) - (2015/06/25) . 在我的项目中,当学生注册时,他选择课程的开始和结束日期,例如(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 . 现在我想做的是,当学生填写表格以提交学费时,他只需要从该课程的年份中选择年份,即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. $ years是一个数组,其中包含用户输入的两个日期之间的所有年份。

Hope this helps. 希望这可以帮助。 Peace! 和平! xD xD

You need to use strtotime() for getting year from start and end date as and than get all years between two years as: 您需要使用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

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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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