Php 相对于当前选定时间段添加的时间
我有一个格式为Php 相对于当前选定时间段添加的时间,php,Php,我有一个格式为dd-mm-yyyy的输入字段purchase\u-date,并选择以下选项 <select name="warranty_period"> <option selected="selected" value="30d">30 days</option> <option value="60d">60 days</option> <option value="90d">90 days</opti
dd-mm-yyyy
的输入字段purchase\u-date
,并选择以下选项
<select name="warranty_period">
<option selected="selected" value="30d">30 days</option>
<option value="60d">60 days</option>
<option value="90d">90 days</option>
<option value="1y">1 year</option>
<option value="2y">2 years</option>
<option value="3y">3 years</option>
<option value="5y">5 years</option>
<option value="unlimited">Unlimited</option>
</select>
30天
60天
90天
一年
两年
三年
五年
无限的
现在我要做的是创建一个新变量
$expires\u on
,其值应该是购买日期+保修期
。30d表示30天,60d表示60天,90d表示90天,依此类推。如何实现这一点?您可以根据select
tag中的所选选项添加inteval
确保您正在使用
您可以使用PHP的对象实现以下目的:
$expires_on= DateTime::createFromFormat('d-m-Y', $purchase_date);
$expires_on->add(new DateInterval('P' . $warranty_period));
echo $expires_on->format('d-m-y');
注意,“D”、“M”或“Y”需要大写,因此可以将选择选项更改为“60D”(etc)使用):
我在这里找到了一个有效的解决方案。给你
$expires_on = '';
if ($data['expires_on']==''){
if ($data['warranty_period']=='30d'){
$expires_on = date('Y-m-d', strtotime($purchase_date. ' + 30 days'));
} else if ($data['warranty_period']=='60d'){
$expires_on = date('Y-m-d', strtotime($purchase_date. ' + 60 days'));
} else if ($data['warranty_period']=='90d'){
$expires_on = date('Y-m-d', strtotime($purchase_date. ' + 90 days'));
} else if ($data['warranty_period']=='1y'){
$expires_on = date('Y-m-d', strtotime($purchase_date. ' + 1 year'));
} else if ($data['warranty_period']=='2y'){
$expires_on = date('Y-m-d', strtotime($purchase_date. ' + 2 years'));
} else if ($data['warranty_period']=='3y'){
$expires_on = date('Y-m-d', strtotime($purchase_date. ' + 3 years'));
} else if ($data['warranty_period']=='5y'){
$expires_on = date('Y-m-d', strtotime($purchase_date. ' + 5 years'));
}
}请找到以下解决方案
$date=日期(“d-m-Y”);
列表($day、$month、$year)=分解('-',$date);
$p_date=mktime(0,0,0,$month,$day,$year)//购买日期
echo“采购日期:”.日期('d-m-Y',$p_日期);
echo“30天:“.$data=日期('d-m-Y',标准时间(日期('Y-m-d',$p_日期)。”+30天”);
echo“60天:“.$data=日期('d-m-Y',标准时间(日期('Y-m-d',$p_日期)。”+60天”);
echo“90天:“.$data=日期('d-m-Y',标准时间(日期('Y-m-d',$p_日期)。”+90天”);
echo“1年:“.$data=日期('d-m-Y',标准时间(日期('Y-m-d',$p_日期)。”+1年”);
echo“2年:“.$data=日期('d-m-Y',标准时间(日期('Y-m-d',$p_日期)。”+2年”);
echo“3年:“.$data=日期('d-m-Y',标准时间(日期('Y-m-d',$p_日期)。”+3年”);
echo“5年:“.$data=日期('d-m-Y',标准时间(日期('Y-m-d',$p_日期)。”+5年”);
$expires_on->add(new DateInterval('P' . strtoupper($warranty_period)));
$expires_on = '';
if ($data['expires_on']==''){
if ($data['warranty_period']=='30d'){
$expires_on = date('Y-m-d', strtotime($purchase_date. ' + 30 days'));
} else if ($data['warranty_period']=='60d'){
$expires_on = date('Y-m-d', strtotime($purchase_date. ' + 60 days'));
} else if ($data['warranty_period']=='90d'){
$expires_on = date('Y-m-d', strtotime($purchase_date. ' + 90 days'));
} else if ($data['warranty_period']=='1y'){
$expires_on = date('Y-m-d', strtotime($purchase_date. ' + 1 year'));
} else if ($data['warranty_period']=='2y'){
$expires_on = date('Y-m-d', strtotime($purchase_date. ' + 2 years'));
} else if ($data['warranty_period']=='3y'){
$expires_on = date('Y-m-d', strtotime($purchase_date. ' + 3 years'));
} else if ($data['warranty_period']=='5y'){
$expires_on = date('Y-m-d', strtotime($purchase_date. ' + 5 years'));
}
Please find the below solution
$date = date("d-m-Y");
list($day, $month, $year) = explode('-', $date);
$p_date = mktime(0, 0, 0, $month, $day, $year); //Purchase Date
echo "Purchase Date: ".date('d-m-Y',$p_date);
echo "</br>30 Days: ".$data = date('d-m-Y',strtotime(date("Y-m-d", $p_date) . " + 30 day"));
echo "</br>60 Days: ".$data = date('d-m-Y',strtotime(date("Y-m-d", $p_date) . " + 60 day"));
echo "</br>90 Days: ".$data = date('d-m-Y',strtotime(date("Y-m-d", $p_date) . " + 90 day"));
echo "</br>1 Year: ".$data = date('d-m-Y',strtotime(date("Y-m-d", $p_date) . " + 1 year"));
echo "</br>2 Years: ".$data = date('d-m-Y',strtotime(date("Y-m-d", $p_date) . " + 2 year"));
echo "</br>3 Years: ".$data = date('d-m-Y',strtotime(date("Y-m-d", $p_date) . " + 3 year"));
echo "</br>5 Years: ".$data = date('d-m-Y',strtotime(date("Y-m-d", $p_date) . " + 5 year"));