php 每次循环运行时递增日期的代码

php code to increment date on every loop runs

在我的优惠券栏中,数据是这样的 500,501,502 OK..

优惠券列中有 3 个值,因此我在数据库中插入 3 行每个优惠券值..

我需要在每个循环运行中插入 coupondate 也是月份的递增顺序...假设上面的循环运行 3 次,然后 3 个 coupondate 被插入到 coupondate 列中。

就是这样...这就是为什么我在那里使用循环...

请帮助解决我的问题。 我需要将每个日期都插入到数据库中,而不是最后一个。

$coupon = $_POST['coupon'];                     
                            $arr = explode(",", $coupon);
                            $min = min($arr);
                            $max = max($arr);
                            $startingdate = $_POST['startingdate'];                         
                            for ($i = 1; $i <= $max; $i++)
                            {                       
                             $coupondate = date("d-m-Y", strtotime(date("d-m-Y", strtotime($startingdate)) . " +" . $i .  "month")); 
                            for ($i = $min; $i <= $max; $i++)
                            {               
                $insertrow  = $database->insertRow("INSERT INTO book_issue (coupondate,coupon) VALUES (:coupondate,:coupon)", array(':coupondate'=>$coupondate,':coupon'=>$i));
           }
}

检查下面的代码你错过了递增的月份值

for($i = $min; $i <= $max; $i++) {
    $dates[] = $coupon . " - " . date("d-m-Y", strtotime($startingdate . " +" . $i . " MONTHS -1 DAYS"));        
}


echo "<pre>";
print_r($dates); //out put you required
echo "</pre>";

OOP 方式:

$startingdate = '26-02-2015';
$date = new DateTime($startingdate);
$diff = new DateInterval('P1M');
for($i = 0; $i < 5; $i++) {
    $date->add($diff);
    print_r($date);
}

DateTime

试试这个:

$startingdate = '26-02-2015';

$min = 0;
$max = 2;

for($i = $min; $i <= $max; $i++) {
    $dates[] = date("d-m-Y", strtotime($startingdate . " +" . ($i+1) . " MONTHS"));        
}


var_dump($dates); 
$coupon = $_POST['coupon'];                     
$arr = explode(",", $coupon);
$min = min($arr);
$max = max($arr);
$startingdate = date("d-m-Y", strtotime($_POST['startingdate']));
for ($i = 1 ; $i <= count($arr) ; $i++) {
    $count = 1;
    for ($ii = $min; $ii <= $max; $ii++) {
        $coupondate = date("d-m-Y", strtotime(date("d-m-Y", strtotime($startingdate)) . " +" . $count . " month"));
        $count++;
        $insertrow  = $database->insertRow("INSERT INTO book_issue (coupondate) VALUES (:coupondate)", array(':coupondate'=>$coupondate));
    }
}