如何获得前一天(连续)

时间:2017-08-13 09:24:21

标签: javascript php html

大家好我想要获取上一个日期或最后一天的信息,只有当我按下按钮时它会在最后一天(星期六)显示所有信息,如果我再次点击该按钮,它将显示最后一天的信息(星期五)如果我再次点击它(星期四)感谢帮助我们

编辑:

generate_attendance.php

if(isset($_POST['submit1'])){
                                 $prev_date= date('Y/m/d',strtotime("-1 days"));


                                    $query=mysqli_query($dbcon,"select * from attendance where date_added '$prev_date'")or die(mysql_error());
                                    while($row=mysqli_fetch_array($query)){

                                    $attendance_id=$row['attendance_id'];


                                    ?>

                                    <tr>
                                       <td><?php echo $row['lastname'].', '.$row['firstname']; ?></td>
                                       <td><?php echo $row['course']; ?></td>
                                       <td><?php echo $row['type']; ?></td>
                                       <td><?php echo $row['year_level']; ?></td>                                           <td><?php echo $row['date_added']; ?</td>                                
                                    </tr>
                                    <?php

            <div class="controls">


                            <button name="submit1" type="submit1" class="btn btn-success"><i class="icon-plus-sign icon-large"></i> Previous Day</button>
                </div>
            </div>

generate_attendance.php(完整代码)

<div class="container">
    <div class="margin-top">
        <div class="row">   
                            <div class="alert alert-info">
                                <button type="button" class="close" data-dismiss="alert">&times;</button>
                                <strong><i class="icon-user icon-large"></i>&nbsp;Attendance Report</strong>
                            </div>

    <div class="span12">    
       <center class="title">
                    <h1>Attendance List</h1>
                    </center>

                            <div class="pull-right">
                            <a href="" onclick="window.print()" class="btn btn-info"><i class="icon-print icon-large"></i> Print</a>
                            </div>  
    <form method="post">
    <div class="span3">



                                        <div class="control-group">
            <label class="control-label" for="inputEmail"><!-- Attendance Report --></label>
            <div class="controls">
            <label class="control-label" for="inputEmail">From</label>
                <div class="controls">
                <input type="date" name="from_date" id="date1" alt="date" class="IP_calendar" title="d/m/Y">
                </div>
            </div>
        </div>
            <div class="control-group"> 
                <label class="control-label" for="inputEmail">To Date</label>
                <div class="controls">
                    <input type="date" name="to_date" id="date2" alt="date" class="IP_calendar" title="d/m/Y">
            <!--    <input type="text"  class="w8em format-d-m-y highlight-days-67 range-low-today" name="due_date" id="sd" maxlength="10" style="border: 3px double #CCCCCC;" required/> -->
                </div>
            </div>
            <div class="control-group"> 
                <div class="controls">

                            <button name="submit" type="submit" class="btn btn-success"><i class="icon-plus-sign icon-large"></i> Search</button>
                </div>
            </div>
            <div class="controls">


                            <button name="submit1" type="submit1" class="btn btn-success"><i class="icon-minus-sign icon-large"></i> Previous Day</button>
                </div>
            </div>



            <div class="span8">
                    <div class="alert alert-success"><strong>Attendance Report</strong></div>
                        <table cellpadding="0" cellspacing="0" border="0" class="table" id="example">

                            <thead>
                                <tr>



                                        <th>Name</th>
                                        <th>Program Code</th>
                                        <th>Type</th>
                                        <th>Year level</th>
                                        <th>Date Log-in</th>

                                </tr>
                            </thead>
                           <tbody>
                                <?php
                    if(isset($_POST['submit'])){
                                    $from_date=$_POST['from_date'];
                                    $to_date=$_POST['to_date'];

                                    $query=mysqli_query($dbcon,"select * from attendance where date_added between '$from_date' and '$to_date'")or die(mysql_error());
                                    while($row=mysqli_fetch_array($query)){

                                    $attendance_id=$row['attendance_id'];


                                    ?>

                                    <tr>
                                       <td><?php echo $row['lastname'].', '.$row['firstname']; ?></td>
                                       <td><?php echo $row['course']; ?></td>
                                       <td><?php echo $row['type']; ?></td>
                                       <td><?php echo $row['year_level']; ?></td>
                                       <td><?php echo $row['date_added']; ?></td>

                                    </tr>
                                    <?php
                                    }}
?>

       <?php


                    if(isset($_POST['submit1'])){



                                      $prev_date= date('Y/m/d',strtotime("-1 days"));

                                    $query=mysqli_query($dbcon,"select * from attendance where date_added between '$curr_date' and '$prev_date'")or die(mysql_error());
                                    while($row=mysqli_fetch_array($query)){

                                    $attendance_id=$row['attendance_id'];


                                    ?>

                                    <tr>
                                       <td><?php echo $row['lastname'].', '.$row['firstname']; ?></td>
                                       <td><?php echo $row['course']; ?></td>
                                       <td><?php echo $row['type']; ?></td>
                                       <td><?php echo $row['year_level']; ?></td>
                                       <td><?php echo $row['date_added']; ?></td>

                                    </tr>
                                    <?php
                                    }}
                                    ?>

                            </tbody>
                        </table>

            </form>
        </div>      
        </div>      

        </div>
    </div>
</div>

3 个答案:

答案 0 :(得分:1)

可能会传递一个查询参数,该参数指示应该减去多少日期,因此网址如下所示:

yourphpscript.php? days = 3

然后你可以在php中获得它,并更改日期构建:

$days = $_GET["days"];
if(!isset($days)){
 $days = 1;
}
$days = intval($days);
$prev_date= date('Y/m/d',strtotime("-".$days." days"));

所以现在唯一缺少的就是更改下一个请求的url,如下所示:

<form href="?days=<?php echo $days+1;?>" >

答案 1 :(得分:0)

您可以在POST数据中将当前显示的日期发送回服务器。

<input type="hidden" name="currently_displayed" value="<?php echo $prev_date ?>">
<button name="submit1" type="submit1" class="btn btn-success"><i class="icon-plus-sign icon-large"></i> Previous Day</button>

然后在你的PHP中:

if (isset($_POST['currently_displayed'])) {
    // Convert as unix-timestamp
    $currentlyDisplayed = strtotime($_POST['currently_displayed']);
} else {
    // Not posted, assuming user wants to see yesterday (thus $currentlyDisplayed should be today/now)
    $currentlyDisplayed = time();
}
// strtotime( $formatString, $relativeTo );
$prev_date= date('Y/m/d',strtotime("-1 days", $currentlyDisplayed));

答案 2 :(得分:0)

我建议您使用javascript / JQuery AJAX请求,您可以立即更改它,并且您可以跟踪您前几天的点击次数!你不再需要ifset POST submit1。

  1. 在按钮上&#34; Submit1&#34; ,具有发送numberPrevDays值的AJAX请求的onclick函数。确保跟踪此次点击(哎呀,你甚至可以在第二天使用另一个按钮)。有几种方法可以做到这一点。在发送到AJAX请求之前,要么只有一个将继续递增(或递减)的全局变量

  2. 有一个单独的文件&amp;函数php,它接收Prev天数。此文件将处理数据并输出响应json您的SQL mysqli_fetch_array数据

  3. 使用该输出响应,只需清空并使用新数据重新加载表

  4. 抱歉,我没有写实际代码