Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/298.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 递增字符串以获取下一个月和下一年_Php_Date_Time - Fatal编程技术网

Php 递增字符串以获取下一个月和下一年

Php 递增字符串以获取下一个月和下一年,php,date,time,Php,Date,Time,我有两个变量,分别引用月数和年数: $month = $_REQUEST['month']; $month1 = sprintf("%02s", $month);; $year1 = $_REQUEST['year']; $date1 = $month1.'/01/'.$year1; 这一切都按预期进行。我现在需要得到下个月的类似变量。用简单的英语来说,这相当于: $month2 = $month1 + 1; // e.g. if $month1 = 12, $month2 = 01 and

我有两个变量,分别引用月数和年数:

$month = $_REQUEST['month'];
$month1 = sprintf("%02s", $month);;

$year1 = $_REQUEST['year'];
$date1 = $month1.'/01/'.$year1;
这一切都按预期进行。我现在需要得到下个月的类似变量。用简单的英语来说,这相当于:

$month2 = $month1 + 1; // e.g. if $month1 = 12, $month2 = 01 and $year2 = $year1 + 1

我不相信
$date1
是所需的日期格式,因此我不确定是否可以使用任何日期函数,或者使用数学来增加数字是否更简单?

使用
日期时间

$dt = DateTime::createFromFormat('m/d/Y', sprintf('%d/01/%d',
        $_REQUEST['month'], $_REQUEST['year']));

$interval = new DateInterval('P1M'); // 1 month

echo $dt->add($interval)->format('m/d/Y');
使用(适用于PHP>=5.2.0):

$month=$\u请求['month'];
$year=$_请求['year'];
$date=新日期时间($year-$month-1”);
$date->modify(“+1个月”);
echo“下个月是:”,$date->格式('Y-m');

您是否尝试过将
日期
标准时间
组合使用?@zllalani噢,请注意,PHP 5.3.0于2009年6月30日发布。我不应该一直这么说