【问题标题】:Having trouble wrapping my head around how to write a particular loop无法解决如何编写特定循环的问题
【发布时间】:2020-06-10 19:41:01
【问题描述】:

我正在查询 SQL 数据库以返回未完成的工单操作。
我的查询是生成工单号、操作状态和截止日期。
我试图弄清楚如何遍历返回的数组并且:

  • 收集一周内到期的操作总和
  • 收集第二周到期的操作总和
  • 继续这样做,直到我通过所有条目,并根据需要持续数周。

我的 SQL 查询类似于:

SELECT * FROM OPERATION WHERE RESOURCE_ID = '280LASERS' ORDER BY DUE_DATE;

它会返回如下内容:

W/O #   | Setup Hours  |  Run Hours  |  Due Date
W159769 |     0.5      |    15.0     | 03/01/2020
W159770 |     1.5      |     9.0     | 04/01/2020
W159771 |     0.75     |    81.0     | 05/01/2020

无论哪种方式,我想要完成的是,查询数据库,逐步检查我的结果并获得每周的总和。
而现在 + 7 天

第一周 = 15.5 小时;第二周 = 10.5 小时;第三周 = 81.75 小时

编辑: 对于我的问题一团糟,我深表歉意,这是我尝试使用 SQL 和 PHP 完成的更紧张的任务之一。

我们正在努力更好地处理我们的容量,并报告我们的容量。
我希望能够运行一个查询来提取所有“280LASERS”操作并进行某种排序根值(如今天的日期)来比较 DUE_DATE。

我的计划是按 DUE_DATE 排序并获得 SUM(SETUP_HRS + RUN_HRS) 直到 DUE_DATE 大于 (TODAY() + 7) 然后,获取 SUM(SETUP_HRS + RUN_HRS) 直到 DUE_DATE 大于 (TODAY() + 14) 然后 ...

我无法使用静态变量来实现这一点,因为周数可以从 6 周开始, 超过 30 周,这取决于最远订单的 DUE_DATE。

我是如此接近我可以品尝它,我真的很想分享我的代码和输出......但是感觉我把这个页面炸了,真是一团糟。我是否可以删除上面的所有内容并按原样转发我的代码以及我得到的输出。

【问题讨论】:

  • 我对表格示例中的截止日期与预期输出之间的相关性感到有些困惑。截止日期相隔一个月。你的意思是让他们相隔一周吗?
  • 花点时间阅读帮助中心的editing help。 Stack Overflow 上的格式化与其他站点上的不同。您的帖子看起来越好,其他人就越容易阅读和理解。

标签: php sql loops output


【解决方案1】:

我认为您不需要循环。看起来您可以只计算周数,按此分组,然后将一周的小时数相加。

MySQL

select
  DATEDIFF(due_date, NOW()) DIV 7 + 1 AS week_number,
  SUM(setup_hours + run_hours) AS week_hours
from operation
where resource_id = '280LASERS'
group by week_number;

SQL 服务器

select
  DATEDIFF(wk, getdate(), due_date) AS week_number,
  SUM(setup_hours + run_hours) AS week_hours
from operation
where resource_id = '280LASERS'
group by DATEDIFF(wk, getdate(), due_date);

【讨论】:

  • 我正在尝试在 Microsoft SQL Server Management Studio 中执行此代码,但出现此错误:“'NOW' 不是可识别的内置函数名称。”
  • 好的,我将 NOW() 换成 CURRENT_TIMESTAMP 并且得到 DATEDIFF 需要 3 个参数。
  • 好的,我将那行更改为“DATEDIFF(week, WORK_ORDER.DESIRED_WANT_DATE, CURRENT_TIMESTAMP) AS week_number”,我正在慢慢取得进展。
  • 哦,糟糕,这是 SQL Server。我假设 MySQL 是因为它经常与 PHP 一起使用。我建议使用您正在使用的特定系统标记与数据库相关的问题。
  • 我希望我能弄清楚如何转换它并让它工作,因为它更干净、更简单。感谢您的意见!
【解决方案2】:

所以我回来了,我将在这里添加一个更好的注释代码:无法编辑旧答案,因为我删除了我的帐户并忘记取消:|

无论如何,您问的是如何操作数据。这是一个简单的数组,所有内部数组都是从一周开始到结束的总和。现在,你可以用不同的键来存储它们,为了简单起见,我只使用了默认分配。

$results=array(
  array( 'due_date'=>'12/02/2020', 'run_hours'=>12.4, 'setup_hours'=>2.4, ),   //  2020-02-12 00:00:00
  array( 'due_date'=>'15/02/2020', 'run_hours'=>10.4, 'setup_hours'=>1.4, ),   //  2020-02-15 00:00:00
  array( 'due_date'=>'18/02/2020', 'run_hours'=>8.4, 'setup_hours'=>3.4, ),    //  2020-02-18 00:00:00
  array( 'due_date'=>'20/02/2020', 'run_hours'=>2.4, 'setup_hours'=>1.4, ),    //  2020-02-20 00:00:00
  array( 'due_date'=>'21/02/2020', 'run_hours'=>9.4, 'setup_hours'=>1.4, ),    //  2020-02-21 00:00:00
  array( 'due_date'=>'24/02/2020', 'run_hours'=>12.4, 'setup_hours'=>1.4, ),   //  2020-02-24 00:00:00
  array( 'due_date'=>'26/02/2020', 'run_hours'=>11.3, 'setup_hours'=>1.4, ),   //  2020-02-26 00:00:00
  array( 'due_date'=>'29/02/2020', 'run_hours'=>4.4, 'setup_hours'=>2.4, ),    //  2020-02-29 00:00:00
  array( 'due_date'=>'02/03/2020', 'run_hours'=>5.7, 'setup_hours'=>4, ),      //  2020-03-02 00:00:00
  array( 'due_date'=>'04/03/2020', 'run_hours'=>11.5, 'setup_hours'=>3.4, ),   //  2020-03-04 00:00:00
  array( 'due_date'=>'06/03/2020', 'run_hours'=>7.3, 'setup_hours'=>1.4, ),    //  2020-03-06 00:00:00
  array( 'due_date'=>'08/03/2020', 'run_hours'=>9.6, 'setup_hours'=>1.4, ),    //  2020-03-08 00:00:00
  array( 'due_date'=>'12/03/2020', 'run_hours'=>14.7, 'setup_hours'=>1.4, ),   //  2020-03-12 00:00:00
  array( 'due_date'=>'15/03/2020', 'run_hours'=>12.5, 'setup_hours'=>1.4, ),   //  2020-03-15 00:00:00
  array( 'due_date'=>'19/03/2020', 'run_hours'=>4.4, 'setup_hours'=>1.4, ),    //  2020-03-19 00:00:00
  array( 'due_date'=>'21/03/2020', 'run_hours'=>5.6, 'setup_hours'=>4, ),      //  2020-03-21 00:00:00
  array( 'due_date'=>'24/03/2020', 'run_hours'=>11.4, 'setup_hours'=>1.4, ),   //  2020-03-24 00:00:00
  array( 'due_date'=>'29/03/2020', 'run_hours'=>7.4, 'setup_hours'=>1.4, ),    //  2020-03-29 00:00:00
  array( 'due_date'=>'01/04/2020', 'run_hours'=>9.4, 'setup_hours'=>1.4, ),    //  2020-04-01 00:00:00

  // some far off weeks
  array( 'due_date'=>'18/06/2020', 'run_hours'=>9.4, 'setup_hours'=>1.4, ),
  array( 'due_date'=>'21/06/2020', 'run_hours'=>9.4, 'setup_hours'=>1.4, ),
  array( 'due_date'=>'09/07/2020', 'run_hours'=>9.4, 'setup_hours'=>1.4, ),
  array( 'due_date'=>'12/08/2020', 'run_hours'=>9.4, 'setup_hours'=>1.4, ),


);
$time=strtotime(date('Y-m-d')); // get time in same
// wrapping time in strtotime and date trims the seconds to the desired format

$one_week=60*60*24*7;
$sums=array();
foreach($results as $row){
/* php 5.3+ this block of code gets the time of the date, this conversions are made in case a custom non standard date format is made, alternatively one can use strtotime with the correct date format

 $date = DateTime::createFromFormat('d/m/Y', $row['due_date']);//use your format and values
 if(!$date){
  echo 'Not a valid format';
  break;
 }
 $entry_time = strtotime(date('Y-m-d',$date->getTimestamp()));
 // if your date format doesnt have hours minutes and seconds then timestamp will add the current h,min,s,
 // this may not be desired, so this wrapping it in strtotime and date trims the values
*/
 // WARNING:  If the format is right weeks will be way off
 $entry_time = strtotime($row['due_date']); // if due_date is a valid format, see PHP docs for more information
 if (!$entry_time) {
   echo "Not a valid date format";
   break;
 }
 $entry_work_hours=$row['run_hours']+$row['setup_hours'];
 // if the entry time is by some reason smaller then the current time save it to a special past_due container
 if ($entry_time < $time) {
   // if a past_due container exists add the sum, otherwise create a past_due container
   if (isset($sums['past_due'])) {
     $sums['past_due']['sum']+=$entry_work_hours;
   } else {
     $sums['past_due']= array(
      'sum'   => $entry_work_hours,
      'start' => $row['due_date'], // the earliest event
      'end'   => date('d/m/Y',$time), // current time, if $entry_time is bigger or equal we're talking about entries that are yet to happen
     );
   }
 } else if ( $entry_time >= $time ){
   // getting the future_dues array, every object holds an array/map, that holds the sum, the start of the week and when the week ends
   // endings are exclusive ie. if an entry_data falls on the end date it goes to the start of the next container
   if (isset($sums['future_dues'])) {
     $future_dues=$sums['future_dues'];
   } else {
     $future_dues = array(
       array(
         'sum'   => 0,
         'start' => $time,
         'end'   => $time+$one_week
       )
     );
   }
   // get the last week container, and save the key so we can reassign it back to the $sums array on the right spot
   $last_index = count($future_dues)-1;
   $future_due = $future_dues[$last_index];

   // manipulate the week data
   // if the entry time is smaller then the current end of the week add to the sum, otherwise add a new week interval container
   if ($entry_time < $future_due['end']) {
     $future_due['sum']+=$entry_work_hours;
     // reassign week container
     $future_dues[$last_index]=$future_due;
   } else {
     $last_week_end = $future_due['end'];
     $new_end       = $last_week_end + $one_week;

     //do a while loop to get the next week end in which the work is done
     while ($new_end < $entry_time) {
       // skip this part if empty weeks are not desired
       $future_dues[] = array(
         'sum'   => 0,
         'start' => $last_week_end,
         'end'   => $new_end
       );
       $last_week_end = $new_end;
       $new_end       = $new_end + $one_week;
       // echo "$new_end < $entry_time".'<br>';
     }

     // add a new week container, the start of the week is the end of the previous one and the end is 7 days from that
     $future_dues[]=array(
       'sum'   => $entry_work_hours,
       'start' => $last_week_end,
       'end'   => $new_end
     );
   }
   // reassign the whole week containers container to the array
   $sums['future_dues']=$future_dues;
 }
}
// convert time back to dates
foreach ($sums['future_dues'] as $key => &$due) {
  $due['start']=date('d/m/Y',$due['start']);
  $due['end']=date('d/m/Y',$due['end']);
}

// use $sums to display the values you need, use:
// echo "<pre>";
// print_r($sums);
// echo "</pre>";
// to better understand how data is stored


echo "<pre>"; // use pre tags to have a nice inline values, this can be rewriten into a table
$past_due=$sums['past_due'];
//past due is a single container
$time_prefix="Time: ";
$working_hours_prefix="Working hours: ";

$time = $time_prefix.$sums['past_due']['start']." - ".$sums['past_due']['end'];
echo $time."<br>";
echo $working_hours_prefix.str_pad($sums['past_due']['sum'],abs(strlen($time)-strlen($working_hours_prefix)),' ',STR_PAD_LEFT);
// make it inline with the time
echo "<br><br>";

$due_dates=$sums['future_dues'];
foreach($due_dates as $week_container){
  $time = $time_prefix.$week_container['start']." - ".$week_container['end'];
  echo $time."<br>";
  echo $working_hours_prefix.str_pad($week_container['sum'],abs(strlen($time)-strlen($working_hours_prefix)),' ',STR_PAD_LEFT);
  echo "<br><br>";

  //echo $week_container['sum'];      /// if you want to show the sum
  //echo $week_container['start'];    /// if you want to show the start
  //echo $week_container['end'];      /// if you want to show the end
}
echo "</pre>";
// above is a bit abstracted but it esencially does this


echo "<br><br>";
echo "<br><br>";

$past_due=$sums['past_due'];

$past_start = $sums['past_due']['start'];
$past_end   = $sums['past_due']['end'];
$past_sum   = $sums['past_due']['sum'];

echo "Time: $past_start - $past_end<br>";
echo "Working hours: $past_sum"; // previous case adds breaks to be inline
echo "<br><br>";

$due_dates=$sums['future_dues'];
foreach($due_dates as $week_container){

  $week_start = $week_container['start'];
  $week_end   = $week_container['end'];
  $week_sum   = $week_container['sum'];

  echo "Time: $week_start - $week_end<br>";
  echo "Working hours: $week_sum"; // previous case adds breaks to be inline
  echo "<br><br>";

}

编辑: 添加了一个新的 while 循环来解决空周的问题。 注意 d/m/Y 不是 strtotime 可识别的格式,它将被读取为 m/d/Y。转换it refer to this question.

编辑-2: 回答您的评论。好的,所以关于跨度的事情是我制作了它们,以便如果跨度从 2020-01-01 到 2020-01-08 并且第二个从 2020-01-08 到 2020-01-15 应该在哪里工作2020-01-08 的时间转到第 1 周还是第 2 周? 当您将 $entry_time

如果您希望容器跨越 2020-01-01 和 2020-01-08 和 2020-01-09 和 2020-01-16 之间,并且两端都为包括的。现在,我不打算写这部分,因为它真的取决于你想要如何定义你的结局。

您的问题是您是否可以将时间更改为当前时间以外的其他时间?当然,只需更改此行。

$time=strtotime(date('Y-m-d'));

//to

$time=__TIME__YOU_WANT_IN_SECONDS__;

//or

$time = strtotime(__THE_DATE_YOU_WANT__); // eg. 01/01/2020
// now this is the time to compare all other dates to

【讨论】:

  • 再次感谢您的回复,该代码是一些很棒的工作。我唯一遇到麻烦的地方是那些还没有价值的星期。例如,我们已经有 2020 年 6 月 4 日的订单,但在 2020 年 4 月 30 日和 2020 年 6 月 3 日之间没有订单,因此在中间的几周内的输出仍然输出(它不会跳过空周)并且将从下一个预定周拉出第一个条目,导致许多问题。所以,我可以使用直到那个时候的数据,但它都是不正确的。
  • 啊哈,我明白了,给我一两分钟
  • 嘿,什么会导致这一天少一个呢?它说它将于 03/06 结束,但实际上它将于 03/05 结束。我想我需要在某个地方 +1,但看不到。
  • P.S.它似乎在第一次迭代后自行修复,所以 PAST DUE 是正确的,第一周跨度是 -1 天,第二周跨度是 +1 天,那么接下来几周的所有数字都是正确的。
  • 我做到了!问题在于这一行: if ($entry_time
【解决方案3】:

正如 Don't Panic 指出的截止日期,我认为您可能会以 dd/mm/YYYY 格式显示它,我将从那里开始工作。 我将使用 foreach 循环,但我认为 Don't Panic 的解决方案可能更有效。

步骤: 1.遍历值并计算日期的时间, 2.将其与所需日期进行比较 3. 将其添加到正确的总和中

注意:日期格式很重要,如果缺少小时、分钟和秒,getTimestamp 会为其添加当前值

我根据对问题的理解添加了一些测试数据。

我还添加了一个过期列,以防任何日期已经超过当前时间()。

foreach 循环检查 entry_time 是否小于当前时间,这意味着它已过期。

如果没有,我们检查future_dues 是否设置。未来会费代表未来所有不同的周。如果没有设置一个数组,则添加一个数组。 第二个数组表示从当前时间到未来 7 天结束的当前最近的未来。

开始和结束有助于我们更好地阅读最终结果,它们是时间戳。还添加了一个值为 0 的 sum 键。

然后我们从future_dues 中取出最后一个元素,看看due_date 是否小于周末。如果是我们添加工作时间,否则我们添加一个新的 future_due 对象。

最后我添加了一个将时间戳转换为日期格式的 foreach 循环。


$results=array(
  array( 'due_date'=>'12/02/2020', 'run_hours'=>12.4, 'setup_hours'=>2.4, ), //  2020-02-12 00:00:00
  array( 'due_date'=>'15/02/2020', 'run_hours'=>10.4, 'setup_hours'=>1.4, ), //  2020-02-15 00:00:00
  array( 'due_date'=>'18/02/2020', 'run_hours'=>8.4, 'setup_hours'=>3.4, ), //  2020-02-18 00:00:00
  array( 'due_date'=>'20/02/2020', 'run_hours'=>2.4, 'setup_hours'=>1.4, ), //  2020-02-20 00:00:00
  array( 'due_date'=>'21/02/2020', 'run_hours'=>9.4, 'setup_hours'=>1.4, ), //  2020-02-21 00:00:00
  array( 'due_date'=>'24/02/2020', 'run_hours'=>12.4, 'setup_hours'=>1.4, ), //  2020-02-24 00:00:00
  array( 'due_date'=>'26/02/2020', 'run_hours'=>11.3, 'setup_hours'=>1.4, ), //  2020-02-26 00:00:00
  array( 'due_date'=>'29/02/2020', 'run_hours'=>4.4, 'setup_hours'=>2.4, ), //  2020-02-29 00:00:00
  array( 'due_date'=>'02/03/2020', 'run_hours'=>5.7, 'setup_hours'=>4, ), //  2020-03-02 00:00:00
  array( 'due_date'=>'04/03/2020', 'run_hours'=>11.5, 'setup_hours'=>3.4, ), //  2020-03-04 00:00:00
  array( 'due_date'=>'06/03/2020', 'run_hours'=>7.3, 'setup_hours'=>1.4, ), //  2020-03-06 00:00:00
  array( 'due_date'=>'08/03/2020', 'run_hours'=>9.6, 'setup_hours'=>1.4, ), //  2020-03-08 00:00:00
  array( 'due_date'=>'12/03/2020', 'run_hours'=>14.7, 'setup_hours'=>1.4, ), //  2020-03-12 00:00:00
  array( 'due_date'=>'15/03/2020', 'run_hours'=>12.5, 'setup_hours'=>1.4, ), //  2020-03-15 00:00:00
  array( 'due_date'=>'19/03/2020', 'run_hours'=>4.4, 'setup_hours'=>1.4, ), //  2020-03-19 00:00:00
  array( 'due_date'=>'21/03/2020', 'run_hours'=>5.6, 'setup_hours'=>4, ), //  2020-03-21 00:00:00
  array( 'due_date'=>'24/03/2020', 'run_hours'=>11.4, 'setup_hours'=>1.4, ), //  2020-03-24 00:00:00
  array( 'due_date'=>'29/03/2020', 'run_hours'=>7.4, 'setup_hours'=>1.4, ), //  2020-03-29 00:00:00
  array( 'due_date'=>'01/04/2020', 'run_hours'=>9.4, 'setup_hours'=>1.4, ), //  2020-04-01 00:00:00

);
$time=strtotime(date('Y-m-d')); // get time in same
// wrapping time in strtotime and date trims the seconds to the desired format

$one_week=60*60*24*7;
$sums=array();
foreach($results as $row){
 $date = DateTime::createFromFormat('d/m/Y', $row['due_date']);//use your format and values
 if(!$date){
  echo 'Not a valid format';
  break;
 }
 $entry_time = strtotime(date('Y-m-d',$date->getTimestamp()));
 // if your date format doesnt have hours minutes and seconds then timestamp will add the current h,min,s,
 // this may not be desired, so this wrapping it in strtotime and date trims the values
 $entry_work_hours=$row['run_hours']+$row['setup_hours'];
 if ($entry_time < $time) {
   if (isset($sums['future_dues'])) {
     $sums['past_due']['sum']+=$entry_work_hours;
   } else {
     $sums['past_due']= array(
      'sum'=> $entry_work_hours,
      'start'=> $row['due_date'],
      'end' => date('d/m/Y',$time),
     );
   }
 } else if ( $entry_time > $time ){
   if (isset($sums['future_dues'])) {
     $future_dues=$sums['future_dues'];
   } else {
     $future_dues = array(
       array(
         'sum'=>0,
         'start'=>$time,
         'end'=>$time+$one_week
       )
     );
   }
   $last_index = count($future_dues)-1;
   $future_due = $future_dues[$last_index];
   if ($entry_time < $future_due['end']) {
     $future_due['sum']+=$entry_work_hours;
     $future_dues[$last_index]=$future_due;
   } else {
     $future_dues[]=array(
       'sum'=>$entry_work_hours,
       'start'=>$future_due['end'],
       'end'=>$future_due['end']+$one_week
     );
   }
   $sums['future_dues']=$future_dues;
 }
}
// if you want to conver them back to dates
foreach ($sums['future_dues'] as $key => &$due) {
  $due['start']=date('d/m/Y',$due['start']);
  $due['end']=date('d/m/Y',$due['end']);
}

【讨论】:

  • 您好,感谢您的回复,我正在阅读并试图理解这一切,我会在完成后回复。再次感谢!
  • 所以,我想我明白了,我输入了我所有的信息。我只是不确定我需要从哪里回显,才能将一些结果发布到页面上......
  • 好的,我可以使用一些帮助,我有 PHP 5.2.1,不知道这会是一个问题,你的大部分代码不兼容(即 DateTime::createFromFormat)。您是否可以提供该代码的替代方案?
【解决方案4】:

SQL 代码似乎给了我一些很好的信息,这是最终版本
SELECT DATEDIFF(week, getdate(), WORK_ORDER.DESIRED_WANT_DATE) AS week_number, SUM(OPERATION.SETUP_HRS + OPERATION.RUN_HRS) AS week_hours FROM OPERATION JOIN WORK_ORDER ON OPERATION.WORKORDER_BASE_ID = WORK_ORDER.BASE_ID WHERE OPERATION.RESOURCE_ID = '103TURRET' AND (OPERATION.STATUS = 'R' OR OPERATION.STATUS = 'F') AND WORK_ORDER.SUB_ID = '0' GROUP BY DATEDIFF(week, getdate(), WORK_ORDER.DESIRED_WANT_DATE) ORDER BY week_number;

给我

week_number week_hours
-14 0.630
-11 1.640
-8 1.980
-1 0.540
0 3.820
1 18.500
2 15.090
3 3.410
5 16.490
7 0.890
9 17.950
14 5.000
19 5.000
23 6.750
27 5.000
31 5.000

我手动合计负数 + 零周 = 逾期

非常感谢 @Don't Panic 的所有帮助。

嗯...如果有人能帮我把它弄到它总是在星期一开始的地方,而不是它运行的那一天,我将不胜感激。

【讨论】:

    【解决方案5】:

    好的,这是我目前的代码。我想让它看起来更好,也许不显示数组中的所有不同元素......但我害怕改变更多。

    就我而言,这是一些非常不可思议的代码。

    <?php //CONNECTION SETTING
      $database='VMFG';
      $odbc_name='SQLServer';
      $odbc_user='sa';
      $odbc_password='Password#';
      $con = odbc_connect($odbc_name,$odbc_user,$odbc_password);
    
      $query="SELECT  OPERATION.WORKORDER_BASE_ID AS 'W/O #', /*STATEMENT TO PULL OPERATIONS AND PARSE WORK ORDER DATES*/
          OPERATION.WORKORDER_SUB_ID AS 'SUB ID',
          OPERATION.RESOURCE_ID AS 'RESOURCE ID',
          OPERATION.SETUP_HRS AS 'SETUP HRS',
          OPERATION.RUN AS 'RUN RATE',
          OPERATION.RUN_TYPE AS 'U/M',
          OPERATION.RUN_HRS AS 'RUN TOTAL',
          OPERATION.CALC_START_QTY AS 'START QTY',
          OPERATION.CALC_END_QTY AS 'END QTY',
          OPERATION.COMPLETED_QTY AS 'QTY COMP',
          OPERATION.DEVIATED_QTY AS 'DIFFERENCE',
          OPERATION.ACT_SETUP_HRS AS 'SETUP USED',
          OPERATION.ACT_RUN_HRS AS 'HRS RUN',
          OPERATION.STATUS,
          OPERATION.SETUP_COMPLETED AS 'SETUP COMP',
          WORK_ORDER.DESIRED_WANT_DATE AS 'DUE DATE'
          FROM OPERATION
          JOIN WORK_ORDER ON OPERATION.WORKORDER_BASE_ID=WORK_ORDER.BASE_ID
          WHERE (OPERATION.STATUS = 'R' OR OPERATION.STATUS = 'F')
          AND (OPERATION.RESOURCE_ID = '".$_GET['operation']."')
          AND WORK_ORDER.SUB_ID = '0'
          ORDER BY STATUS DESC, [DUE DATE];";
    ?>
    
    <form method="get">                     <?//FORM TO CHOOSE OPERATION?>
      <table>
        <tr>
          <td>
            <select name="operation">
                <option value="103TURRET">103TURRET</option>
                <option value="104PRESSBRAKES">104PRESSBRAKES</option>
                <option value="280LASERS">280LASERS</option>
                <option value="300WELD">300WELD</option>
                <option value="701POWDERLINE">701POWDERLINE</option>
                <option value="Outside Service">Outside Server</option>
                <option value="ANYOSS">ANY OSS</option>
            </select>
          </td>
          <td><input type="submit" value="Submit" name="action" /></td>
          <td><a href='index.php'>Start Over</a></td>
          <td><a href='/xampp/mpc_db/index.php'>Return to DB</a></td>
        </tr>
      </table>
    </form>
    
    <?php
    $exec = odbc_exec($con, $query);
    $results = array();
    while ($row = odbc_fetch_array($exec)) {
      $results[] = $row;
    }
    //*****************************************************************
    $time=strtotime(date('Y/m/d')); // get time in same
    //wrapping time in strtotime and date trims the seconds to the desired format
    
    $one_week=604800;
    $sums=array();
    foreach($results as $row){
      /*$date = DateTime::createFromFormat('Y/m/d', $row['DUE DATE']);//use your format and values*/
      $date = date($row['DUE DATE']);
    
    if(!$date){
      echo 'Not a valid format';
      break;
    }  
    $entry_time = strtotime(date('Y/m/d',strtotime($date)));
    // if your date format doesnt have hours minutes and seconds then timestamp will add the current h,min,s,
    // this may not be desired, so this wrapping it in strtotime and date trims the values
    $entry_work_hours=$row['RUN TOTAL']+$row['SETUP HRS'];
    
    if ($entry_time < $time) {
    if (isset($sums['past_due'])) {
     $sums['past_due']['sum'] += $entry_work_hours;
     } else {
     $sums['past_due'] = array(
      'sum'=> $entry_work_hours,
      'start'=> $row['due_date'],
      'end' => date('Y/m/d',$time),
     );
    }
    } else if ( $entry_time > $time ){
    if (isset($sums['future_dues'])) {
     $future_dues=$sums['future_dues'];
    } else {
     $future_dues = array(
       array(
         'sum'=>0,
         'start'=>$time,
         'end'=>$time+$one_week
       )
     );
    }
    $last_index = count($future_dues)-1;
    $future_due = $future_dues[$last_index];
    if ($entry_time < $future_due['end']) {
     $future_due['sum']+=$entry_work_hours;
     $future_dues[$last_index]=$future_due;
    } else {
     $future_dues[]=array(
       'sum'=>$entry_work_hours,
       'start'=>$future_due['end'],
       'end'=>$future_due['end']+$one_week
     );
    }
    $sums['future_dues']=$future_dues;
    }
    }
    
    // if you want to convert them back to dates
    foreach ($future_dues as $key => &$due) {
      $due['start']=date('Y/m/d',$due['start']);
      $due['end']=date('Y/m/d',$due['end']);
    }
    
    //**********************************************************************
    foreach($sums['past_due'] as $stuff){ //Actually kind-of sort-of********
      print_r($stuff);                    //Looks like exactly**************
      echo "<br>";                        //What I am trying to create******
    }                                     //Past Due Hours!*****************
    //**********************************************************************
    
    //**********************************************************************
    foreach($future_dues as $edues){  //Actually kind-of sort-of************
      print_r($edues);                //Looks like exactly******************
      echo "<br>";                    //What I am trying to create**********
    }                                 //Future Hours!***********************
    //**********************************************************************
    
    ?>  
    

    最好的部分是!我的输出是可读的!

    5.26
    
    2020/02/27
    Array ( [sum] => 9.26 [start] => 2020/02/27 [end] => 2020/03/05 )
    Array ( [sum] => 7.31 [start] => 2020/03/05 [end] => 2020/03/12 )
    Array ( [sum] => 6.27 [start] => 2020/03/12 [end] => 2020/03/19 )
    Array ( [sum] => 2.14 [start] => 2020/03/19 [end] => 2020/03/26 )
    Array ( [sum] => 11.82 [start] => 2020/03/26 [end] => 2020/04/02 )
    Array ( [sum] => 6.95 [start] => 2020/04/02 [end] => 2020/04/09 )
    Array ( [sum] => 36 [start] => 2020/04/09 [end] => 2020/04/16 )
    Array ( [sum] => 0.81 [start] => 2020/04/16 [end] => 2020/04/23 )
    Array ( [sum] => 30.98 [start] => 2020/04/23 [end] => 2020/04/30 )
    Array ( [sum] => 1.3 [start] => 2020/04/30 [end] => 2020/05/07 )
    Array ( [sum] => 3.29 [start] => 2020/05/07 [end] => 2020/05/14 )
    Array ( [sum] => 1.57 [start] => 2020/05/14 [end] => 2020/05/21 )
    Array ( [sum] => 1.95 [start] => 2020/05/21 [end] => 2020/05/28 )
    Array ( [sum] => 0.29 [start] => 2020/05/28 [end] => 2020/06/04 )
    Array ( [sum] => 2.19 [start] => 2020/06/04 [end] => 2020/06/11 )
    Array ( [sum] => 1.57 [start] => 2020/06/11 [end] => 2020/06/18 )
    Array ( [sum] => 1.95 [start] => 2020/06/18 [end] => 2020/06/25 )
    Array ( [sum] => 1.3 [start] => 2020/06/25 [end] => 2020/07/02 )
    Array ( [sum] => 3.29 [start] => 2020/07/02 [end] => 2020/07/09 )
    Array ( [sum] => 0.67 [start] => 2020/07/09 [end] => 2020/07/16 )
    Array ( [sum] => 0.33 [start] => 2020/07/16 [end] => 2020/07/23 )
    Array ( [sum] => 2.73 [start] => 2020/07/23 [end] => 2020/07/30 )
    Array ( [sum] => 17.79 [start] => 2020/07/30 [end] => 2020/08/06 )
    Array ( [sum] => 1.57 [start] => 2020/08/06 [end] => 2020/08/13 )
    Array ( [sum] => 1.95 [start] => 2020/08/13 [end] => 2020/08/20 )
    Array ( [sum] => 1.3 [start] => 2020/08/20 [end] => 2020/08/27 )
    Array ( [sum] => 3.29 [start] => 2020/08/27 [end] => 2020/09/03 )
    Array ( [sum] => 1.3 [start] => 2020/09/03 [end] => 2020/09/10 )
    Array ( [sum] => 3.29 [start] => 2020/09/10 [end] => 2020/09/17 )
    Array ( [sum] => 1.57 [start] => 2020/09/17 [end] => 2020/09/24 )
    Array ( [sum] => 1.95 [start] => 2020/09/24 [end] => 2020/10/01 )  
    

    虽然我不知道如何在不破坏它的情况下操作它......

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-03-12
      • 1970-01-01
      • 2014-11-13
      • 2019-07-09
      • 2017-06-24
      • 1970-01-01
      • 1970-01-01
      • 2019-01-09
      相关资源
      最近更新 更多