如何从存储在PHP变量中的日期值中获取下一年的日期


how to get the next year date from the date value stored in a php variable

我正试图从一个php变量获得下一年的日期,该变量持有从html表单发布的日期值。下面是代码

    $warranty_from = mysql_real_escape_string($_POST['from_date']);

假设$ waranty_from的值为2013-06-04。我想知道明年的日期,也就是2014-06-04并存储到$warranty_to变量中。在网上搜索了很多,但找不到任何与我的问题相关的资源。任何帮助都是感激的。

下面应该可以工作:

$warranty_from = '2013-06-04';
$warranty_to = date('y-m-d', strtotime('+1 years', strtotime($warranty_from)));

这样应该可以

这就是代码

$ newWarranty_date =日期("Y-m-d strtotime("+ 1",strtotime (warranty_from美元))),

试试这个

<?php
 $warranty_to = date('Y-m-d', strtotime('+1 year', strtotime($warranty_from)));
?>

使用DateTime类进行这样的计算是微不足道的:-

$_POST['from_date'] = '2013-06-04';//Just for this example.
$warranty = DateTime::createFromFormat('Y-m-d', $_POST['from_date']);
$interval = new DateInterval('P1Y');
$warranty->add($interval);
echo $warranty->format('Y-m-d');

将输出:-

2014-06-04