dongya4335 2018-05-18 22:35
浏览 30
已采纳

将接下来的30天放入阵列

I am needing to find the next 30 days and put in array to repeat a scheduling code.

So I would need to have a variable for

$date= "would hold the date"; $day_of_week= "would hold the # value for the day of week";

I am at a loss of where to even start... I am wanting to repeat the following code for each respective day to schedule crews for the next 30 days.

   <?
// 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条回答 默认 最新

  • du512926 2018-05-19 14:42
    关注

    I am not sure this is the best way, however it works.

    <?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>';
                    }
    
    
                }
            }
        }
    }
    }
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 mmocr的训练错误,结果全为0
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀