本文介绍了PHP的PDO代码附加日期没有的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

列名

优惠券

优惠券

开始日期

我正在基于优惠券和开始日期生成优惠券日期.

I am generating coupondate based on coupon and startingdate.

用于EX-

如果优惠券= 500,501,开始日期= 2015年2月24日

if coupon = 500,501 and startingdate = 24-02-2015

然后,优惠券日期将生成如下.

then coupondate generates like below..

coupondate = 23-03-2015,23-04-2015

coupondate = 23-03-2015,23-04-2015

但是我需要在优惠券的日期后附加优惠券

用于EX-

500-2015年3月23日

500 - 23-03-2015

501-2015年2月23日一样明智.

501 - 23-04-2015 like wise..

plz帮助获得以上输出

plz help to get above output

下面是我的代码

$coupon = $_POST['coupon'];                         
                    $startingdate = $_POST['startingdate'];                         
                    $coupons = explode(',', $coupon);                       
                    $dates = Array();
                    for ($no = 1; $no < count($coupons) + 1; $no++) 
                    {
                    $dates[] = date("d-m-Y", strtotime($startingdate . " +" . $no . " MONTHS -1 DAYS"));                        
                    }                       
                    $coupondate = implode(',', $dates); 

推荐答案

尝试使用foreach代替for循环,并在附有日期的同时附加优惠券代码.

Try the foreach instead of for loop and append the coupon code along with the date.

$coupon = $_POST['coupon'];
$startingdate = $_POST['startingdate'];
$coupons = explode(',', $coupon);
$dates = Array();
$no = 1;
foreach($coupons as $coupon) {
    $dates[] = $coupon . " - " . date("d-m-Y", strtotime($startingdate . " +" . $no . " MONTHS -1 DAYS"));
    $no++;
}
$coupondate = implode(',', $dates);

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

这篇关于PHP的PDO代码附加日期没有的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 16:37