我有purchase_date
格式的输入字段dd-mm-yyyy
,以及以下选择
<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>
现在我想做的是我想创建一个新变量$expires_on
,其值应为purchase_date + warranty_period
。 30d表示30天,60d表示60天,90d表示90天,依此类推。我将如何实现这一目标?
答案 0 :(得分:1)
您可以根据$date = new DateTime('2000-01-01');
$interval = new DateInterval('P10D') //add 10 days "PXD" x = no of days
$interval = new DateInterval('P1M'); //add 10 days "PXM" x = no of months
$date->add($interval);
echo $date->format('Y-m-d') . "\n";
代码中的所选选项添加inteval。
确保您使用的是(PHP 5 >= 5.3.0, PHP 7)
{{1}}
答案 1 :(得分:1)
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"));
答案 2 :(得分:0)
您可以使用PHP的DateTime对象:
$expires_on= DateTime::createFromFormat('d-m-Y', $purchase_date);
$expires_on->add(new DateInterval('P' . $warranty_period));
echo $expires_on->format('d-m-y');
注意,&#34; D&#34;,&#34; M&#34;或&#34; Y&#34;需要大写,所以要么将选项更改为&#34; 60D&#34; (等)使用strtoupper()):
$expires_on->add(new DateInterval('P' . strtoupper($warranty_period)));
答案 3 :(得分:0)
我在这里找到了一个有效的解决方案。这是
$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'));
}
}