php pdo代码,用于从所选日期计算下一个6个月的日期
我的表格有两个文本框okphp pdo代码,用于从所选日期计算下一个6个月的日期,php,mysql,date,pdo,Php,Mysql,Date,Pdo,我的表格有两个文本框ok 优惠券,开始日期, 在优惠券文本框中输入的数据,如501502506。 数据类型-varchar startingdate是一个日期选择器- 数据类型-日期时间 现在我需要的是如果我的CoupContextBox有6个值s,并且我在日期选择器中选择了当前日期,那么它会在每个日期中插入6个日期+1个月。 对于ex-我的文本框共有501506个值。我在datepicker中选择2015年2月13日,然后单击save 然后创建如下6个日期并插入coupondate列。 请建议
优惠券,开始日期,
在优惠券文本框中输入的数据,如501502506。
数据类型-varchar
startingdate是一个日期选择器-
数据类型-日期时间
现在我需要的是如果我的CoupContextBox有6个值s,并且我在日期选择器中选择了当前日期,那么它会在每个日期中插入6个日期+1个月。
对于ex-我的文本框共有501506个值。我在datepicker中选择2015年2月13日,然后单击save
然后创建如下6个日期并插入coupondate列。
请建议如何做到这一点
501- 13-03-2015,
502- 13-04-2015,
503- 13-05-2015,
504- 13-06-2015,
505- 13-07-2015
506 - 13-08-2015
下面是我的代码
coupondate数据类型-varchar,因为我们需要以逗号分解形式在同一列中插入所有日期值
<?php
if(isset($_POST['save']))
{
$coupon = $_POST['coupon'];
$startingdate = $_POST['startingdate'];
$coupondate = date("d-m-Y", strtotime($startingdate) . " +1 month");
$insertrow = $database->insertRow("INSERT INTO receipt_entry (coupondate,coupon,startingdate)
VALUES (:coupondate,:coupon,:startingdate)",
array(':coupondate'=>$coupondate,':coupon'=>$coupon,':startingdate'=>$startingdate))
}
?>
我会使用
DateTime
来代替时间函数
$coupon = '501,502,503,504,505,506';
$startingdate = DateTime::createFromFormat('d-m-Y', '13-02-2015');
$coupondate = clone $startingdate;
$coupons = explode(',', $coupon);
$interval = new DateInterval('P1M');
$params = array(
':coupon' => $coupon,
// i assume this is an actual date column and will need to
// in SQL date format
':startingdate' => $startingdate->format('Y-m-d'),
':coupondate' => array()
);
foreach ($coupon as $k => $coupon) {
// add 1 month to the previous coupondate
$coupondate->add($interval);
$params['coupon'][] = $coupondate->format('d-m-Y');
}
// implode out dates to a comma sep list
$params[':coupondate'] = implode(',', $params[':coupondate']);
$database->insertRow("INSERT INTO receipt_entry (coupondate,coupon,startingdate) VALUES (:coupondate,:coupon,:startingdate)", $params);
每件事都正常工作,但请告诉我是否需要像12-03-2015,12-04-2015这样的输出,那么我们在代码中更改了什么。。。我需要所选日期的输出-1天。对于ex,如果我选择2015年2月13日,那么它将生成2015年3月12日、2015年4月12日、2015年5月12日、2015年6月12日,就像wise一样……很高兴听到这样的消息。太糟糕了,虽然你实际上没有把我的答案标记为被接受的答案…:-Psory先生,我错误地接受了另一个解决方案并点击它。非常非常抱歉,现在我接受您的解决方案,再次感谢。没问题,伙计!感谢您的修复,祝您在优惠券项目上好运:)
12-03-2015,12-04-2015,12-05-2015,12-06-2015,12-07-2015,12-08-2015
$coupon = '501,502,503,504,505,506';
$startingdate = DateTime::createFromFormat('d-m-Y', '13-02-2015');
$coupondate = clone $startingdate;
$coupons = explode(',', $coupon);
$interval = new DateInterval('P1M');
$params = array(
':coupon' => $coupon,
// i assume this is an actual date column and will need to
// in SQL date format
':startingdate' => $startingdate->format('Y-m-d'),
':coupondate' => array()
);
foreach ($coupon as $k => $coupon) {
// add 1 month to the previous coupondate
$coupondate->add($interval);
$params['coupon'][] = $coupondate->format('d-m-Y');
}
// implode out dates to a comma sep list
$params[':coupondate'] = implode(',', $params[':coupondate']);
$database->insertRow("INSERT INTO receipt_entry (coupondate,coupon,startingdate) VALUES (:coupondate,:coupon,:startingdate)", $params);