我试图添加X月从我的数据库采取的日期
$sql_batch = "SELECT * FROM mgm_subscription WHERE status = '1'"
$query_batch = mysql_query($sql_batch);
$row_batch = mysql_fetch_array($query_batch);
$addMonth = 3;
$startDate = $row_batch['start_month'];
$endDate = strtotime('+'.$addMonth.' month', $startMonth); // add number of days from form
$endDate = date('m/d/y H:i:s', $endDate );
$sql_date = "INSERT INTO user_subscription (user_id, start_month, end_month, sub_status) VALUES ('".$usercode2."','".$startDate."','".$endDate."', '')";
$query_date = mysql_query($sql_date);
NULL被插入到end_month中。mysql
中的start_month和end_month为DATE类型如何解决这个问题?tq .
如果我理解你的问题,你的$endDate
应该是
$endDate = date('Y-m-d', strtotime($startDate.' +'.$addMonth.' months'));
这就等于:
$endDate = $startDate + 3 months /* in Y-m-d format */
编辑:刚才看到你的列数据类型是Date
。这意味着你不能在你的日期中存储时间戳。它必须是Y-m-d
格式,因为这是有效的mysql格式支持。
您插入$endMonth值(从表单的天数),然后您尝试替换它($endMonth),并回调您的替换变量..
$ addMonth = strtotime('+'.$addMonth。' month', $startMonth);//从表单添加天数$endMonth = date('m/d/y H:i:s', $endMonth);
它将返回空值。我的建议是,尝试放置其他变量以防止重复值或丢失数据
您只提到添加X个月。也许我误解了你的问题,但是如果你只关心月份,我会这样做:
if ($startMonth === 'January') {
$monthNumber = 1;
} else if ($startMonth === 'February') {
$monthNumber = 2;
} //Up to November then finish with {
else {
$monthNumber = 12;//December
}
$newMonthNumber = $monthNumber + $addMonth;
if ($newMonthNumber % 12 == 1) {
$endMonth = 'January';
} else if ($newMonthNumber % 12 == 1) {
$endMonth = 'February';
} //Up to November then finish with {
else {
$endMonth = 'December';
}
$sql_date = "INSERT INTO user_subscription (user_id, start_month, end_month, sub_status) VALUES ('".$usercode2."','".$startMonth."','".$endMonth."', '')";
$query_date = mysql_query($sql_date);