如何使用DRY方法执行此PHP代码?

时间:2017-09-07 03:42:12

标签: php codeigniter dry

我正在尝试从第01天到第30天找出每天的当月销售情况。我一次又一次地使用日期功能。并且每次都使用相同的变量。我的代码很大,看起来非常糟糕。我怎样才能使这段代码更智能,以便我不必编写所有这些代码。所有的事情都可以用一个循环来完成....谢谢

    //first date
    $todaySdate=date("Y-m-01 " );
    $todayEdate=date("Y-m-01 23:59:59 ");
    $this->db->select('*'); 
    $this->db->from('ospos_sales AS A');
    $this->db->join('ospos_sales_payments AS B', 'B.sale_id = A.sale_id', 'left');  
    $this->db->where('A.sale_time >=', $todaySdate);
    $this->db->where('A.sale_time <=', $todayEdate);
    $Todaysales = $this->db->get()->result_array();      

    $todaySale=0;
     foreach($Todaysales as $d)
     {
        $todaySale+=$d['payment_amount'];
     }

     echo $todaySdate.'TK  :' .number_format( $todaySale,"2" )."</br>";

     $startDate=date("Y-m-d" );
     $endDate=date("Y-m-d");


   // second date
     $todaySdate=date("Y-m-02 " );
    $todayEdate=date("Y-m-02 23:59:59");
    $this->db->select('*'); 
    $this->db->from('ospos_sales AS A');
    $this->db->join('ospos_sales_payments AS B', 'B.sale_id = A.sale_id', 'left');  
    $this->db->where('A.sale_time >=', $todaySdate);
    $this->db->where('A.sale_time <=', $todayEdate);
    $Todaysales = $this->db->get()->result_array();      

    $todaySale=0;
     foreach($Todaysales as $d)
     {
        $todaySale+=$d['payment_amount'];
     }

     echo $todaySdate.'TK  :' .number_format( $todaySale,"2" )."</br>";

     $startDate=date("Y-m-d" );
     $endDate=date("Y-m-d");


     //third date
     $todaySdate=date("Y-m-03 " );
    $todayEdate=date("Y-m-03 23:59:59");
    $this->db->select('*'); 
    $this->db->from('ospos_sales AS A');
    $this->db->join('ospos_sales_payments AS B', 'B.sale_id = A.sale_id', 'left');  
    $this->db->where('A.sale_time >=', $todaySdate);
    $this->db->where('A.sale_time <=', $todayEdate);
    $Todaysales = $this->db->get()->result_array();      

    $todaySale=0;
     foreach($Todaysales as $d)
     {
        $todaySale+=$d['payment_amount'];
     }

     echo $todaySdate.'TK  :' .number_format( $todaySale,"2" )."</br>";

     $startDate=date("Y-m-d" );
     $endDate=date("Y-m-d");

     // fourth date date
     $todaySdate=date("Y-m-04 " );
    $todayEdate=date("Y-m-04 23:59:59");
    $this->db->select('*'); 
    $this->db->from('ospos_sales AS A');
    $this->db->join('ospos_sales_payments AS B', 'B.sale_id = A.sale_id', 'left');  
    $this->db->where('A.sale_time >=', $todaySdate);
    $this->db->where('A.sale_time <=', $todayEdate);
    $Todaysales = $this->db->get()->result_array();      

    $todaySale=0;
     foreach($Todaysales as $d)
     {
        $todaySale+=$d['payment_amount'];
     }

     echo $todaySdate.'TK  :' .number_format( $todaySale,"2" )."</br>";

     $startDate=date("Y-m-d" );
     $endDate=date("Y-m-d");

     // Fifth date
     $todaySdate=date("Y-m-05 " );
    $todayEdate=date("Y-m-05 23:59:59");
    $this->db->select('*'); 
    $this->db->from('ospos_sales AS A');
    $this->db->join('ospos_sales_payments AS B', 'B.sale_id = A.sale_id', 'left');  
    $this->db->where('A.sale_time >=', $todaySdate);
    $this->db->where('A.sale_time <=', $todayEdate);
    $Todaysales = $this->db->get()->result_array();      

    $todaySale=0;
     foreach($Todaysales as $d)
     {
        $todaySale+=$d['payment_amount'];
     }

     echo $todaySdate.'TK  :' .number_format( $todaySale,"2" )."</br>";

     $startDate=date("Y-m-d" );
     $endDate=date("Y-m-d");
     ........
      .......
      .......

我的代码的示例输出..

2017-09-01 TK :0.00
2017-09-02 TK :1,186.55
2017-09-03 TK :1,311.45
2017-09-04 TK :0.00
.........
........ .
2017-09-30 TK :0.00

1 个答案:

答案 0 :(得分:0)

这里举例说明如何重构代码。

<?php

function get_today_sales (\DateTime $date) {
    $start_date = $date->setTime(0, 0);
    $end_date = $start_date->add(new DateInterval('P1D'));

    /*    
    $this->db->select('*'); 
    $this->db->from('ospos_sales AS A');
    $this->db->join('ospos_sales_payments AS B', 'B.sale_id = A.sale_id', 'left');  
    $this->db->where('A.sale_time >=', $todaySdate);
    $this->db->where('A.sale_time <=', $todayEdate);
    $Todaysales = $this->db->get()->result_array();      

    $todaySale = 0;
    foreach($Todaysales as $d) {
        $todaySale+=$d['payment_amount'];
    }

    return $today_sale;
    */
}

$start = new DateTime('2017-10-01');
$end = new DateTime('2017-10-20');

$interval = new DateInterval('P1D');
$period = new DatePeriod($start, $interval, $end);


foreach ($period as $day) {
    printf("%s -> %s\n", $day->format('Y-m-d'), get_today_sales($day));
}