如何使用php显示未来的月份和日期?
我需要增加一份休假清单。它应该只增加2年,所以应该只显示2014年和2015年我的休假。我的问题是,当他选择2014年时,我必须显示月份,它不应该显示从1月到5月,如果他选择2015年,它应该显示从1月到12月。我如何根据年份填充月份 在显示2013年的图像中,请忽略这一点,因为我的系统显示了错误的年份 这是我的HTML代码如何使用php显示未来的月份和日期?,php,codeigniter,Php,Codeigniter,我需要增加一份休假清单。它应该只增加2年,所以应该只显示2014年和2015年我的休假。我的问题是,当他选择2014年时,我必须显示月份,它不应该显示从1月到5月,如果他选择2015年,它应该显示从1月到12月。我如何根据年份填充月份 在显示2013年的图像中,请忽略这一点,因为我的系统显示了错误的年份 这是我的HTML代码 <table> <tr> <td colspan="3">Add Leave<
<table>
<tr>
<td colspan="3">Add Leave</td>
</tr>
<tr>
<td width="170" valign="middle" height="40" align="left">Date</td>
<td width="15" valign="middle" align="left">:</td>
<td><select id="frm_day" name="frm_day">
<?php
for($i=1;$i<31;$i++)
{
echo "<option value=".$i.">".$i."</option>";
}
?> </select>
</td>
<td>
<select id="frm_year" name="frm_year">
<option value="<?php echo date("Y");?>"><?php echo date("Y");?></option>
<option value="<?php echo date("Y")+1;?>"><?php echo date("Y")+1;?></option>
</select>
</td>
<td>
<select id="frm_month" name="frm_month">
<option value="<?php echo date("m");?>"><?php echo date("M");?></option>
</select>
</td>
</tr>
<tr>
<td colspan="3">
<input type="submit" name="Submit" id="Submit" value="Submit" class="submit" />
</td>
</tr>
</table>
最简单的一个你可以从 然后您可以使用它:
$timestamp = strtotime("+1 month", yourstartdate);
并使用它
date('Ymd', $timestamp);
您必须在未来的项目上进行循环:
for($i=0;$i < 12; $i++)
{
$timestamp = strtotime("+".$i." months");
if(isInFuture($timestamp) && date('m', $timestamp) <= 12) echo date('m', $timestamp);
}
function isInFuture($date, $timestamp = "")
{
return ($date > date($timestamp));
}
将此添加到您的 和变化:
<select id="frm_year" name="frm_year">
到
如果用户选择,这将删除当前年份以外的月份。在任何其他年份,完整列表都会恢复。我强烈建议在这一年使用日期选择器。一个流行的日期选择器是jQueryUIDatePicker。以下是您试图实现的一个示例: 这包括2年的上限
<link rel="stylesheet" href="//code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css" />
<form method="POST" action="">
<table border="0" cellpadding="10">
<tr>
<th width="100" align="left">Date</th>
<td><input type="text" name="frm_day" id="frm_day" size="1" readonly></td>
<td><input type="text" name="frm_year" id="frm_year" size="5" readonly></td>
<td><input type="text" name="frm_month" id="frm_month" size="3" readonly></td>
<td><img src="http://jqueryui.com/resources/demos/datepicker/images/calendar.gif" id="datepickerImage" style="cursor: pointer;" /></td>
</tr>
</table>
</form>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$('#datepickerImage').on('click', function(){
$("#frm_day").datepicker('show');
});
$('#frm_day, #frm_month, #frm_year').datepicker({
minDate: 0,
maxDate: '+2Y',
changeMonth: true,
changeYear: true,
dateFormat: 'd/yy/M',
onClose: function(dateText){
$(this).val('');
$('#frm_day').val(dateText.split('/')[0]);
$('#frm_month').val( dateText.split('/')[2]);
$('#frm_year').val( dateText.split('/')[1]);
}
});
});
</script>
有关更多信息,请访问。所有的设置都可以在那里找到
您所描述的内容需要JavaScript。为什么必须手动编写此内容?周围有很多挑选日期的人?示例:您的for循环从31开始?六月的时候会发生什么?它只有30天。这并不能回答我如何根据年份填充月份的问题啊,对不起,我错过了一些东西;我将更正我的回答和OP的一个注意事项:您仍然应该验证用PHP发布的日期,因为用户可以很容易地修改这些日期。
<select id="frm_year" name="frm_year" onchange="changedYear(this)">
<link rel="stylesheet" href="//code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css" />
<form method="POST" action="">
<table border="0" cellpadding="10">
<tr>
<th width="100" align="left">Date</th>
<td><input type="text" name="frm_day" id="frm_day" size="1" readonly></td>
<td><input type="text" name="frm_year" id="frm_year" size="5" readonly></td>
<td><input type="text" name="frm_month" id="frm_month" size="3" readonly></td>
<td><img src="http://jqueryui.com/resources/demos/datepicker/images/calendar.gif" id="datepickerImage" style="cursor: pointer;" /></td>
</tr>
</table>
</form>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$('#datepickerImage').on('click', function(){
$("#frm_day").datepicker('show');
});
$('#frm_day, #frm_month, #frm_year').datepicker({
minDate: 0,
maxDate: '+2Y',
changeMonth: true,
changeYear: true,
dateFormat: 'd/yy/M',
onClose: function(dateText){
$(this).val('');
$('#frm_day').val(dateText.split('/')[0]);
$('#frm_month').val( dateText.split('/')[2]);
$('#frm_year').val( dateText.split('/')[1]);
}
});
});
</script>