如何使用while循环求和两个日期之间的所有值?

时间:2019-04-21 21:54:09

标签: php sum increment

我从数据库中获取了一个条目列表,这些条目代表了代表在给定的一天和一周内进行的呼叫次数。我希望销售代表能够根据他在该周的致电量来查看他每周赚多少钱。该周从星期日开始,到星期六结束。通过将他在该周的所有致电加在一起,我如何计算他在给定一周内的总收入?我正在努力解决这个问题,因为我正在努力总结给定一周的所有值。

table.php

  <table>
             <thead>
            <tr>
                <th>Task</th>
                <th>Status</th>
                  <th>Date</th>
                <th>Day</th>
                <th>Earning</th>

            </tr>
        </thead>
            <tbody>
                <?php 
                $i=1;
                $total_weekly_pay=0;
                while ($money = $selectprospectsqueryResult->fetch_assoc()){$i++;


                         $task = 'Outbound call';
                         $status = $money['call_status'];
                         //format dates
                         $date = $money['last_update'];
                         $datetime = new DateTime($date);
                         $day = $datetime->format('l');


                         if($status == 'Success'){

                             $pay = 0.25;
                             $bonus = 5.00;
                             $earning = $pay + $bonus;

                         }else{
                             $pay = 0.25;
                             $earning = $pay;
                         }

                   //get week of date
                  $this_week = date("W", strtotime($date));
                  //get year of date
                  $this_year = date("Y", strtotime($date));

                  //this monday of date
                  $this_monday_date = new DateTime();
                            $this_monday_date->setISODate($this_year,$this_week);
                  $this_monday = $this_monday_date->format('Y-m-d');

                  //this sunday of date
                 //calculate -1 day to find sunday of the same given week
                $sundate = strtotime($this_monday);
                $newsundate = strtotime("-1 day", $sundate);
                $this_sunday = date('Y-m-d', $newsundate);
                //echo ' to ' ;//testing
                  //calculate +6 days to find saturday of the same given week
                              $saturdate = strtotime($this_sunday);
                              $newsaturdate = strtotime("+6 day", $saturdate);
                $this_saturday = date('Y-m-d', $newsaturdate);
                  //echo ' <br> ' ;//testing

                //format the given call date
               $new_date_format = date( 'Y-m-d', strtotime($date)) ;

               //create array of saturdays : could be useful?
              /* $saturdays = array();
               $saturdays[0] = date('Y-m-d', strtotime('first saturday of this month'));
               $saturdays[1] = date('Y-m-d', strtotime('second saturday of this month'));
               $saturdays[2] = date('Y-m-d', strtotime('third saturday of this month'));
               $saturdays[3] = date('Y-m-d', strtotime('fourth saturday of this month'));
               $fifth = strtotime('fifth saturday of this month');
                if (date('m') === date('m', $fifth)) {
                  $saturdays[4] = date('Y-m-d', $fifth);
                }*/

               echo ' <tr>

                       <td>'.$task.'</td>

                        <td>'.$status.'</td>

                         <td>'.$date.'</td>

                        <td>'.$day.'</td>

                        <td>$'.$earning.'</td>

                         </tr>';


              if(week_is_over){

               echo ' <tr>

                       <td>-</td>

                        <td>-</td>

                         <td>From '.$this_sunday.' to '.$this_saturday.'</td>

                        <td>Total for week</td>

                        <td>'.$total_weekly_pay+=$earning.'</td>

                         </tr>';

               }


                }?>

            </tbody>
        </table>

这是我要实现的目标

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js"></script>

<table class="table table-bordered table-condensed table-striped table-hover" width="100%" cellspacing="0">
             <thead>
            <tr>
                <th>Task</th>
                <th>Status</th>
                  <th>Date</th>
                <th>Day</th>
                <th>Earning</th>
              
            </tr>
        </thead>
            <tbody>
             <tr>
                           
                       <td>Outbound call</td>
                       
                        <td>Success</td>
                            
                         <td>2019-04-07 19:05:09</td>
                            
                        <td>Sunday</td>
                            
                        <td>$5.25</td>
                        
                         </tr> 
                         
                          <tr>
                           
                       <td>Outbound call</td>
                       
                        <td>Success</td>
                            
                         <td>2019-04-08 17:05:09</td>
                            
                        <td>Monday</td>
                            
                        <td>$5.25</td>
                        
                         </tr> 
                         
                         <tr>
                           
                       <td>-</td>
                       
                        <td>-</td>
                            
                        <td>From 2019-04-07 to 2019-04-13</td>
                            
                        <td>Total for this week</td>
                            
                        <td>$10.50</td>
                        
                         </tr>
                 <tr>
                           
                       <td>Outbound call</td>
                       
                        <td>No Response</td>
                            
                         <td>2019-04-15 18:38:01</td>
                            
                        <td>Monday</td>
                            
                        <td>$0.25</td>
                        
                         </tr> <tr>
                           
                       <td>Outbound call</td>
                       
                        <td>Busy Line</td>
                            
                         <td>2019-04-15 18:37:57</td>
                            
                        <td>Monday</td>
                            
                        <td>$0.25</td>
                        
                         </tr> <tr>
                           
                       <td>Outbound call</td>
                       
                        <td>Voicemail</td>
                            
                         <td>2019-04-16 18:37:54</td>
                            
                        <td>Tuesday</td>
                            
                        <td>$0.25</td>
                        
                         </tr> <tr>
                           
                       <td>Outbound call</td>
                       
                        <td>Not Interested</td>
                            
                         <td>2019-04-16 18:37:51</td>
                            
                        <td>Tuesday</td>
                            
                        <td>$0.25</td>
                        
                         </tr> <tr>
                           
                       <td>Outbound call</td>
                       
                        <td>Success</td>
                            
                         <td>2019-04-17 18:37:48</td>
                            
                        <td>Wednesday</td>
                            
                        <td>$5.25</td>
                        
                         </tr> <tr>
                           
                       <td>Outbound call</td>
                       
                        <td>Ongoing</td>
                            
                         <td>2019-04-18 18:37:43</td>
                            
                        <td>Thursday</td>
                            
                        <td>$0.25</td>
                        
                         </tr> 
              
              <tr>
                           
                       <td>-</td>
                       
                        <td>-</td>
                            
                         <td>From 2019-04-14 to 2019-04-20</td>
                            
                        <td>Total for this week</td>
                            
                        <td>$6.50</td>
                        
                         </tr>
            </tbody>
        </table>

0 个答案:

没有答案