用于计算列中值的PHP代码,并在日期中添加此值

时间:2015-02-25 12:52:22

标签: php pdo

优惠券= 700,701,702,703

startdate = 25-02-2015

例如 - 现在优惠券栏中有4个值。

我需要什么

当我搜索700时,它选择startdate并添加+1个月来开始更新,因为700位于第一位置并且它生成如下输出

for ex-25-03-2015

当我搜索701时,它会在startdate中获得startdate和+2个月,因为701处于第2位并且它会生成如下所示的输出

for ex-25-04-2015

<?php
    if(!empty($_GET['q'])) {
        $db = new PDO('mysql:host=localhost;dbname=circulation_scheme_prepaid', 'root', '');
        $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $q = $_GET['q'];
        $result = $db->prepare('SELECT * FROM receipt_entry WHERE coupon  = :coupon'); 
        $result->execute(array(':coupon' => "$q"));    
        $data = $result->fetchAll(PDO::FETCH_ASSOC); 
        $info = array();
        foreach($data as $row) {                        
            $startingdate = $row['startingdate'];   
$coupon = $row['coupon'];       
            $generateddate = $row['coupondate'];            
            $cWeb = $row['customer_name'];
            $receipt = $row['receipt_no'];
            $book = $row['book_no'];
            $booking = $row['bookingdate']; 
                  $info[] = array('web' => $cWeb,'rec' =>$receipt,'book' =>$book,'booking' =>$booking,'date' =>$generateddate );
        }
        echo json_encode($info);    
    }
?>

1 个答案:

答案 0 :(得分:0)

您必须自定义您的解决方案,请参阅以下示例:

$coupon = '700,701,702,703';
$startdate = '25-02-2015'; 
$startdate = date_create("25-02-2015");

$find = '702';

$array_coupon = explode(',', $coupon);
$add = array_search($find, $array_coupon) + 1;

date_add($startdate, date_interval_create_from_date_string("$add Months"));
echo date_format($startdate, 'd-m-Y');

编辑:在您的代码中应该是这样的:

<?php
        if(!empty($_GET['q'])) {
           $db = new PDO('mysql:host=localhost;dbname=circulation_scheme_prepaid', 'root', '');
            $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            $q = $_GET['q'];
            $result = $db->prepare('SELECT * FROM receipt_entry WHERE coupon  = :coupon'); 
            $result->execute(array(':coupon' => "$q"));    
            $data = $result->fetchAll(PDO::FETCH_ASSOC); 
            $info = array();
            foreach($data as $row) {                        
            $startingdate = $row['startingdate'];   
            $coupon = $row['coupon'];       
            $generateddate = $row['coupondate'];            
            $cWeb = $row['customer_name'];
            $receipt = $row['receipt_no'];
            $book = $row['book_no'];
            $booking = $row['bookingdate']; 

            $startdate = date_create($startingdate);
            $find = $_GET['q'];

            $array_coupon = explode(',', $coupon);
            $add = array_search($find, $array_coupon) + 1;

            date_add($startdate, date_interval_create_from_date_string("$add Months"));
            echo date_format($startdate, 'd-m-Y');

                  $info[] = array('web' => $cWeb,'rec' =>$receipt,'book' =>$book,'booking' =>$booking,'date' =>$generateddate );
            }
            echo json_encode($info);    
        }
        ?>