将接下来的30天放入阵列

时间:2018-05-18 22:35:10

标签: php arrays calendar

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

所以我需要为

设一个变量

$ date ="会保留日期&#34 ;; $ day_of_week ="将保留星期几的#值&#34 ;;

我不知道从哪里开始......我想在每个日子重复以下代码,以安排接下来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>';
}
} 
        }
    }

2 个答案:

答案 0 :(得分:0)

您最好的选择是提供开始日期和结束日期,并获取它们之间的天数。然后,这只是循环每一天并将其分配给数组的问题。也许可以使用相同的逻辑来解决周。

示例:

/home/user

答案 1 :(得分:0)

我不确定这是最好的方式,但它有效。

<?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>';
                }


            }
        }
    }
}
}
?>