Php 如何每周获取数据。只有周一到下周日

Php 如何每周获取数据。只有周一到下周日,php,mysql,date,weekend,Php,Mysql,Date,Weekend,我已经编写了一个程序,在其中我可以得到过去7天的数据。但我希望我的数据应该是上周的可见数据,从周一开始到周日结束。然后下星期一开始。 所以我想知道今天是否是星期三,如果我点击周报。那么它应该只在上周一之前获得数据。像星期三、星期二、星期一,但我的系统计算完整的7天。 这是我的密码 date_default_timezone_set("EST"); $wdate = date('Y-m-d'); $wdate_to = $wdate; $wdate_to = strtotime("-7 days"

我已经编写了一个程序,在其中我可以得到过去7天的数据。但我希望我的数据应该是上周的可见数据,从周一开始到周日结束。然后下星期一开始。 所以我想知道今天是否是星期三,如果我点击周报。那么它应该只在上周一之前获得数据。像星期三、星期二、星期一,但我的系统计算完整的7天。 这是我的密码

date_default_timezone_set("EST");
$wdate = date('Y-m-d');
$wdate_to = $wdate;
$wdate_to = strtotime("-7 days", strtotime($wdate_to)); //-7 days for last week. -30 for last week
$wdate_to = date("Y-m-d", $wdate_to);


$wget_req = "SELECT * FROM `requests` WHERE `assign_date` between '$wdate_to' AND '$wdate' AND `assigned`=1";

$result = mysqli_query($dbc, $wget_req);

while ($res = mysqli_fetch_assoc($result)){
    $driver_id = $res['assigned_driver_id'];
    $req_id = $res['req_id'];
    $req_title = $res['request_title'];
    $req_price = $res['price'];
    $req_time = $res['request_time'];
    $req_date = $res['assign_date'];
    $req_desc = $res['req_desc'];
    $assigned = $res['assigned'];
    $status = $res['req_status'];
    $driver = $res['driver_name'];
$get_record = "SELECT * FROM `members` WHERE `Memberid`='$driver_id' AND `u_level`=3";
$res = mysqli_query($dbc, $get_record);
while($row = mysqli_fetch_assoc($res)){

    $status = $row['u_status'];
}
?>

                                        <tr class="gradeA">
                                            <td><b><?php echo $driver ;?></b></td>
                                            <td><?php echo $driver_id ;?></td>
                                            <td><?php echo $req_date ;?></td>
                                            <td><b><?php echo $wdate_to;?></b> &#8596; <b><?php echo $wdate ;?></b></td>
                                            <td class="center"><?php if($status == 1){echo '<a href="#" class="btn btn-danger btn-sm">Driver is not working. Deactivated</a>';}else if($status == 0){echo '<a href="#" class="btn btn-success btn-sm">Driver is Active. Working</a>';}?></td>
                                            <td class="center"> <a href="weekly_report.php?driver_id=<?php echo $driver_id ;?>" class="btn btn-primary btn-sm">View Report</a>
 </td>
                                        </tr>

<?php } ?> 
我希望很快能得到帮助

您可以尝试以下方法:

//Returns values 0-6, 0 (for Sunday) through 6 (for Saturday))
$currentDay = date('w') - 1;

//Your week start on Monday, so if today is Sunday, you need to adjust the value
if($currentDay < 0) {
    $currentDay = 6;
}
$weekStartTimestamp = time() - $currentDay * 24 * 60 * 60;
$wdate_to = date("Y-m-d", $weekStartTimestamp);

试试这个,它可能对你有帮助

<?php
date_default_timezone_set("EST");
$wdate = date('Y-m-d',strtotime('monday this week'));
$wdate_to = $wdate;
$wdate_to = strtotime("-7 days", strtotime($wdate_to)); //-7 days for last week. -30 for last week
$wdate_to = date("Y-m-d", $wdate_to);


$wget_req = "SELECT * FROM `requests` WHERE `assign_date` between '$wdate_to' AND '$wdate' AND `assigned`=1";

$result = mysqli_query($dbc, $wget_req);

while ($res = mysqli_fetch_assoc($result)){
    $driver_id = $res['assigned_driver_id'];
    $req_id = $res['req_id'];
    $req_title = $res['request_title'];
    $req_price = $res['price'];
    $req_time = $res['request_time'];
    $req_date = $res['assign_date'];
    $req_desc = $res['req_desc'];
    $assigned = $res['assigned'];
    $status = $res['req_status'];
    $driver = $res['driver_name'];
$get_record = "SELECT * FROM `members` WHERE `Memberid`='$driver_id' AND `u_level`=3";
$res = mysqli_query($dbc, $get_record);
while($row = mysqli_fetch_assoc($res)){

    $status = $row['u_status'];
}
?>

                                        <tr class="gradeA">
                                            <td><b><?php echo $driver ;?></b></td>
                                            <td><?php echo $driver_id ;?></td>
                                            <td><?php echo $req_date ;?></td>
                                            <td><b><?php echo $wdate_to;?></b> &#8596; <b><?php echo $wdate ;?></b></td>
                                            <td class="center"><?php if($status == 1){echo '<a href="#" class="btn btn-danger btn-sm">Driver is not working. Deactivated</a>';}else if($status == 0){echo '<a href="#" class="btn btn-success btn-sm">Driver is Active. Working</a>';}?></td>
                                            <td class="center"> <a href="weekly_report.php?driver_id=<?php echo $driver_id ;?>" class="btn btn-primary btn-sm">View Report</a>
 </td>
                                        </tr>

<?php } ?> 

从给定日期获取星期一日期。这里我给出的日期是2015年7月15日

echo date("Y-m-d", strtotime("previous monday", strtotime("2015-07-15")));
请尝试以下代码:

$currentDay = date('w');
$curDate = date('d-m-Y');
//$curDate = date('d-m-Y', strtotime("-1 week")); // uncomment this line if u want to get data of last week
$mon = $curDate;
if($currentDay > 1){
    $day = $currentDay-1;
    $mon = date('Y-m-d', strtotime($curDate. ' - '.$day.' days'));
}

$sat = $curDate;
if($currentDay !=7){
    $day = (6 -$currentDay);
    $sat = date('Y-m-d', strtotime($curDate. ' + '.$day.' days'));
}
echo $mon;
echo '<br>';
echo $sat;

当前代码有什么问题?也许这有用?它显示了过去7天的数据。。我要数据到上周一,我是说从周一开始到周日结束。所以它应该在上周一之前获取数据。请尝试上面链接的SQL答案。很抱歉,为什么$currentDay=6;这意味着我们正在命令系统今天是一周的第6天??您的一周从周一$currentDay=0开始,最后一天是周日$currentDay=6。因此,如果今天是星期天,您必须从当前时间戳中减去6天,才能得到星期一的时间戳。这很有效。。谢谢你做了很多@Dhingy