2017.8.22学习日志
继昨天学习怎么阅读和调试PHP代码之后,今天的效率快了很多,很多改动知道从哪地方下手。
那么今天的任务是按时间搜索每个人的工单情况,如愿以偿解决了问题。
首先我在需要添加功能的页面添加两个datepicker:
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<script>
$( function() {
$( "#datepicker1" ).datepicker();
} );
$( function() {
$( "#datepicker2" ).datepicker();
} );
</script>
<form action="" method="get">
<div align="center">起止时间:<input type="text" id="datepicker1" name="datepicker1">
至 <input type="text" id="datepicker2" name="datepicker2">
<input type="submit" value="开始">
</div>
</form>
我把datepicker的数据传递到本页面让PHP处理,值得一提的是,我是用的phpstorm开发工具,当我把提交方式设为”post”的时候,php接收良好,可以处理,可是以”get”方式传递则无法接收,调试echo打印 REQUEST的结果是空白,所以我使用php自带的方法把url后带的参数提取出来,进行explode分割得到参数对,再用urldecode取得k−v值数组。一下我就列出"get"方法,"post"只需要简单的提交和 _POST即可。
<?php
$string= $_SERVER['QUERY_STRING'];
$strarr = explode("&",$string);
foreach($strarr as $id=>$arr) {
$r=explode("=",$arr);
$_GET[$r[0]]=urldecode($r[1]);
}
$datepicker1 = $_GET['datepicker1'];
echo $datepicker1;
$datepicker2 = $_GET['datepicker2'];
echo $datepicker2;
//或者直接用post传递参数也可,不知道为什么get不能传数据。
?>
取得参数后,与已有的开始时间和完成时间对比,当开始和完成都在 datepicker1和 datepicker2之间时,才显示出来,即我需要的按时间搜索功能。
<?php
foreach($tasks as $task):
$d1 = date("y-m-d",strtotime($task->finishedDate));
if(strtotime($task->realStarted)>=strtotime($datepicker1)&&strtotime($d1)<=strtotime($datepicker2)&&strtotime($d1)>=strtotime($datepicker1)&&strtotime($d1)<=strtotime($datepicker2)||strtotime($datepicker1)==0||strtotime($datepicker2)==0){
?>
<tr class='text-center'>
<td><?php echo html::a($this->createLink('task', 'view', "taskID=$task->id"), sprintf('%03d', $task->id));?></td>
<td><span class='<?php echo 'pri' . zget($lang->task->priList, $task->pri, $task->pri);?>'><?php echo $task->pri == '0' ? '' : zget($lang->task->priList, $task->pri, $task->pri)?></span></td>
<td class='nobr'><?php echo html::a($this->createLink('project', 'browse', "projectid=$task->projectID"), $task->projectName);?></td>
<td class='nobr'><?php echo html::a($this->createLink('task', 'view', "taskID=$task->id"), $task->name);?></td>
<td><?php echo $task->estimate;?></td>
<td><?php echo $task->consumed;?></td>
<td><?php echo $task->estimate-$task->consumed;?></td>
<td><?php echo $task->realStarted;?></td>
<td class=<?php if(isset($task->delay)) echo 'delayed';?>><?php if(substr($task->deadline, 0, 4) > 0) echo $task->deadline;?></td>
<td><?php echo $task->finishedDate;?></td>
<td class='<?php echo $task->status;?>'><?php echo $lang->task->statusList[$task->status];?></td>
</tr>
<?php $countestimate += $task->estimate;
$countconsumed += $task->consumed;
$countleft += $task->estimate-$task->consumed;
++$counttask;}
endforeach;?>
重点是时间对比大小,需要使用到php中的strtotime方法,把日期转换成时间戳形式,1502441119这种来对比大小,通过 d1=date("y−m−d",strtotime( task->finishedDate))方法去除后面的时分秒。
以上是今天的总结。