Php 将未来30天放入阵列

Php 将未来30天放入阵列,php,arrays,calendar,Php,Arrays,Calendar,我需要找到接下来的30天,并放入数组以重复一个调度代码 所以我需要有一个变量 $date=“将保留该日期”; $day_of_week=“将保留周中某一天的#值” 我甚至不知道从哪里开始。。。我想在每一天重复下面的代码,为接下来的30天安排工作人员 <? // Connection Script include 'connection.php'; date_default_timezone_set("America/New_York"); $tomorrow= strtotime

我需要找到接下来的30天,并放入数组以重复一个调度代码

所以我需要有一个变量

$date=“将保留该日期”; $day_of_week=“将保留周中某一天的#值”

我甚至不知道从哪里开始。。。我想在每一天重复下面的代码,为接下来的30天安排工作人员

   <?
// Connection Script
include 'connection.php';

date_default_timezone_set("America/New_York");

$tomorrow= strtotime('+ 1 day');
$date= date('N', $tomorrow);
// Get all employees who work tomorrow and group by unit//
$units= "select e.user_id, e.station, e.full_name, max(e.level) level, es.unit, es.days, es.start_time, es.end_time from employees e
                 left join employee_schedule es on es.pid = e.user_id
                 where es.days like '%$date%' and e.status = 1
                 group by es.unit";
$units_result= $conn->query($units);
//Roll through all employees who work tomorrow and place then in appropriate unit.
while($row_unit = $units_result->fetch_assoc()) {

    if($row_unit['level'] == 3){
        $level= 1;
    }elseif($row_unit['level'] == 4){
        $level= 2;
    }elseif($row_unit['level'] == 5){
        $level= 3;
    }elseif($row_unit['level'] == 8){
        $level= 4;
    }
    //Get Unit ID from each group
    $unitid = $row_unit['unit'];
   $intime= date('Y-m-d', $tomorrow);
   $intime= $intime.' '. $row_unit['start_time'];
   $length= '+ 23 hours';
   $intimes= strtotime("$intime");
   $endtime= strtotime("$length","$intimes" );
   $endtime= date('Y-m-d H:i:s', $endtime);
   $station= $row_unit['station'];


   $timenow= date('Y-m-d H:i:s');

    echo "<p>I am scheduling unit number $unitid to be on at $intime and leave at $endtime </p>";

    $unitinsert= "insert into schedules (date_time, unit, level_of_service, start_time, end_time, station)
                   values ('$timenow', $unitid, $level , '$intime', '$endtime', $station)";


    if(mysqli_query($conn, $unitinsert)){
    echo "Records inserted successfully.";
    $unitinid= $conn->insert_id;
    echo $unitinid;
} else{
    echo "ERROR: Could not able to execute $unitinsert. " . mysqli_error($conn);
}


    $employee = "select e.*, es.unit, es.days, pc.email as pemail from employees e
                 left join employee_schedule es on es.pid = e.user_id
                 left join phone_carriers pc on pc.id = e.phone_carrier
                 where es.days like '%$date%'and es.unit = $unitid and e.status = 1";
    $employee_result= $conn->query($employee);

        if(mysqli_num_rows($employee_result) > 0){

            while($row_employee = $employee_result->fetch_assoc()) {
                $pid = $unitinid;
                $eid= $row_employee['user_id'];
                $ephone = $row_employee['mobile_number'];
                $emailphone= $row_employee['mobile_number'].''. $row_employee['pemail'];
                $unitcrewinsert= "insert into crew_assignment (date_time, pid, crew_member, phone_number, message_number, confirmed)
                   values ('$timenow', $pid, $eid , '$ephone', '$emailphone', 0)";

                   if(mysqli_query($conn, $unitcrewinsert)){
    echo "Records inserted successfully.";

    echo '<p> Crew Inserted </p>';
} else{
    echo "<p>ERROR: Could not able to execute $unitinsert. " . mysqli_error($conn).'</p>';
}
} 
        }
    }

您最好提供开始日期和结束日期,并获取两者之间的天数。然后,只需每天循环并将其分配给一个数组。也可以用同样的逻辑来解决这几周的问题

例如:

$d1 = Carbon::parse($start_date);
$d2 = Carbon::parse($end_date);

$num_days = $d1->diffInDays($d2) + 1;

$dates = array();
for ($i = 1; $i <= $num_days; $i++) {
    array_push($dates, date('Y-m-d', strtotime($start_date.' +'.$i.' days')));
}

var_dump($dates);
$d1=Carbon::parse($start\u date);
$d2=碳::解析($end_date);
$num_days=$d1->diffinddays($d2)+1;
$dates=array();

对于($i=1;$i我不确定这是否是最好的方法,但它确实有效

<?php

include 'connection.php';

date_default_timezone_set("America/New_York");

// Start date
$date = '2018-05-18';
// End date
$end_date = '2018-05-31';

while (strtotime($date) <= strtotime($end_date)) {



    $date = date("Y-m-d", strtotime("+1 day", strtotime($date)));

    $dayn = date('N', strtotime($date));
    echo "<p> $date </p>";
    echo "<p> $dayn </p>";

    $units = "select e.user_id, e.station, e.full_name, max(e.level) level, es.unit, es.days, es.start_time, es.total_hours from employees e
                 left join employee_schedule es on es.pid = e.user_id
                 where es.days like '%$dayn%' and e.status = 1
                 group by es.unit";
    $units_result = $conn->query($units);
//Roll through all employees who work tomorrow and place then in appropriate unit.
if($units_result->num_rows > 0){
        while ($row_unit = $units_result->fetch_assoc()) {

        if ($row_unit['level'] == 3) {
            $level = 1;
        } elseif ($row_unit['level'] == 4) {
            $level = 2;
        } elseif ($row_unit['level'] == 5) {
            $level = 3;
        } elseif ($row_unit['level'] == 8) {
            $level = 4;
        }
        //Get Unit ID from each group
        $unitid = $row_unit['unit'];

        $intime = $date . ' ' . $row_unit['start_time'];
        $total_hours= $row_unit['total_hours'];
        $length = "+ $total_hours ";
        $intimes = strtotime("$intime");
        $endtime = strtotime("$length", "$intimes");
        $endtime = date('Y-m-d H:i:s', $endtime);
        $station = $row_unit['station'];


        $timenow = date('Y-m-d H:i:s');

        echo "<p>I am scheduling unit number $unitid to be on at $intime and leave at $total_hours </p>";

        $unitinsert = "insert into schedules (date_time, unit, level_of_service, start_time, total_hours, station)
                   values ('$timenow', $unitid, $level , '$intime', '$total_hours', $station)";


        if (mysqli_query($conn, $unitinsert)) {
            echo "Records inserted successfully.";
            $unitinid = $conn->insert_id;
            echo $unitinid;
        } else {
            echo "ERROR: Could not able to execute $unitinsert. " . mysqli_error($conn);
        }

        $inspectioninsert= ""


        $employee = "select e.*, es.unit, es.days, pc.email as pemail from employees e
                 left join employee_schedule es on es.pid = e.user_id
                 left join phone_carriers pc on pc.id = e.phone_carrier
                 where es.days like '%$dayn%'and es.unit = $unitid and e.status = 1";
        $employee_result = $conn->query($employee);

        if (mysqli_num_rows($employee_result) > 0) {

            while ($row_employee = $employee_result->fetch_assoc()) {
                $pid = $unitinid;
                $eid = $row_employee['user_id'];
                $ephone = $row_employee['mobile_number'];
                $emailphone = $row_employee['mobile_number'] . '' . $row_employee['pemail'];
                $unitcrewinsert = "insert into crew_assignment (date_time, pid, crew_member, phone_number, message_number, confirmed)
                   values ('$timenow', $pid, $eid , '$ephone', '$emailphone', 0)";

                if (mysqli_query($conn, $unitcrewinsert)) {
                    echo "Records inserted successfully.";

                    echo '<p> Crew Inserted </p>';
                } else {
                    echo "<p>ERROR: Could not able to execute $unitinsert. " . mysqli_error($conn) . '</p>';
                }


            }
        }
    }
}
}
?>