正如标题所述,我想用"2011-11-2","2011-11-05"哪一个是日期开始日期结束
<?php echo getWorkingDays("2011-11-2","2011-11-05",$holidays); ?>;
如何将两个日期之间的固定值从工作日计算PHP函数更改为变量,并使用Javascript事件处理程序显示输出?
当用户从日期选择器中从文本框"DateFrom"one_answers";DateTo"然后用户需要按查看天数按钮来计算指定的两个日期之间的天数。
下面是获取两个日期之间工作日的PHP函数:
<?php
function getWorkingDays($startDate,$endDate,$holidays){
$days = (strtotime($endDate) - strtotime($startDate)) / 86400 + 1;
$no_full_weeks = floor($days / 7);
$no_remaining_days = fmod($days, 7);
$the_first_day_of_week = date("N", strtotime($startDate));
$the_last_day_of_week = date("N", strtotime($endDate));
if ($the_first_day_of_week <= $the_last_day_of_week) {
if ($the_first_day_of_week <= 6 && 6 <= $the_last_day_of_week) $no_remaining_days--;
if ($the_first_day_of_week <= 7 && 7 <= $the_last_day_of_week) $no_remaining_days--;
}
else {
if ($the_first_day_of_week == 7) {
$no_remaining_days--;
if ($the_last_day_of_week == 6) {
$no_remaining_days--;
}
}
else {
$no_remaining_days -= 2;
}
}
$workingDays = $no_full_weeks * 5;
if ($no_remaining_days > 0 )
{
$workingDays += $no_remaining_days;
}
foreach($holidays as $holiday){
$time_stamp=strtotime($holiday);
if (strtotime($startDate) <= $time_stamp && $time_stamp <= strtotime($endDate) && date("N",$time_stamp) != 6 && date("N",$time_stamp) != 7)
$workingDays--;
}
return $workingDays;
}
$holidays=array("");
?>
当我回显date的固定值时,它工作得很好
<?php echo getWorkingDays("2011-11-2","2011-11-05",$holidays); ?>;
后面跟着javascript和form:
<link rel="stylesheet" type="text/css" href="tcal.css" />
<script type="text/javascript" src="tcal.js">
</script>
<form name=fname method='post'>
<tr valign="baseline">
<td nowrap="nowrap" align="right"> </td>
<td nowrap="nowrap" align="right"><div align="left">Date From:</div></td>
<td><div>
<input id="dateFrom" type="text" name="DateFrom" class="tcal" value='' autocomplete="off" />
</div></td>
</tr>
<tr valign="baseline">
<td nowrap="nowrap" align="right"> </td>
<td nowrap="nowrap" align="right"><div align="left">Date To:</div></td>
<td><div>
<input id="dateTo" type="text" name="DateTo" class="tcal" value="" autocomplete="off"/>
</div></td>
</tr>
<p>
<input id="leavetakens" name="leavetakens" type="text" value="" readonly="readonly">
<input type="button" value="Check Day(s)" name="btnCalculate" onClick="abc()"></p>
</form>
<script type="text/javascript">
function abc()
{
var jsvar = <?php echo getWorkingDays("2011-11-2","2011-11-05",$holidays); ?>;
var dateFrom = document.getElementById("dateFrom").value;
var dateTo = document.getElementById("dateTo").value;
document.fname.leavetakens.value=jsvar
}
</script>
在这一点上,我只能显示从固定的两个日期指定的值,但如何使其作为变量?
<?php $workday=getWorkingDays("2011-11-2","2011-11-05",$holidays); ?>;
似乎你想从客户端/JavaScript端调用服务器/PHP函数(即getWorkingDays()
)。您可以通过abc()
中的AJAX实现这一点。
完整的AJAX可能是这样的:
var dateFrom = document.getElementById("dateFrom").value;
var dateTo = document.getElementById("dateTo").value;
var xmlhttp;
if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
}
else { // code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.fname.leavetakens.value = xmlhttp.responseText;
}
}
xmlhttp.open("GET", "getWorkingDays.php?dateFrom="+dateFrom+"&dateTo="+dateTo, true);
xmlhttp.send();
其中getWorkingDays.php在查询字符串中取dateFrom
和dateTo
两个参数,调用PHP的getWorkingDays()
函数,返回/写入值
如果你使用jQuery(或其他JavaScript库),它将大大节省你的时间和代码:
var dateFrom = $("#dateFrom").val();
var dateTo = $("#dateTo").val();
$.get('getWorkingDays.php', {dateFrom: dateFrom, dateTo: dateTo}, function(data) {
$('#leavetakens').val(data);
});
getWorkingDays.php应该像这样:
<?php echo getWorkingDays($_GET["dateFrom"],$_GET["dateTo"],$holidays); ?>;