Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/241.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 从上一个日期返回X个月_Php_Date_Strtotime - Fatal编程技术网

Php 从上一个日期返回X个月

Php 从上一个日期返回X个月,php,date,strtotime,Php,Date,Strtotime,下面的代码为我提供了从今天开始到2012年11月1日结束的X个月的日期,但我希望得到从2012年11月1日开始到2012年11月1日结束的X个月的日期。如何做到这一点 // $nrOfMonths can be 1, 3 and 6 function GetIncidents(nrOfMonths) { $stopDate = strtotime('-' . $nrOfMonths .' months'); ... rest of the code ... } 您只需执行

下面的代码为我提供了从今天开始到2012年11月1日结束的X个月的日期,但我希望得到从2012年11月1日开始到2012年11月1日结束的X个月的日期。如何做到这一点

// $nrOfMonths can be 1, 3 and 6

function GetIncidents(nrOfMonths) {

    $stopDate = strtotime('-' . $nrOfMonths .' months');

    ... rest of the code ...
}
您只需执行以下操作:

$stopDate = strtotime('1st November 2012 -' . $nrOfMonths .' months');
尽管如此,我还是喜欢这种语法:

$stopDate = strtotime("1st November 2012 - {$nrOfMonths} months");
因此,您的代码应该遵循以下模式:

function GetIncidents(nrOfMonths) {

    //your preferred syntax!

    //the rest of your code

}
您只需执行以下操作:

$stopDate = strtotime('1st November 2012 -' . $nrOfMonths .' months');
尽管如此,我还是喜欢这种语法:

$stopDate = strtotime("1st November 2012 - {$nrOfMonths} months");
因此,您的代码应该遵循以下模式:

function GetIncidents(nrOfMonths) {

    //your preferred syntax!

    //the rest of your code

}

使用
DateTime
DateInterval
类来实现这一点

$date = new DateTime('November 1, 2012');
$interval = new DateInterval('P1M'); // A month

for($i = 1; $i <= $nrOfMonths; $i++) {
    $date->sub($interval); // Subtract 1 month from the date object
    echo $i . " month(s) prior to November 1, 2012 was " . $date->format('F j, Y');
}
$date=新日期时间('2012年11月1日');
$interval=new DateInterval('P1M');//一个月
对于($i=1;$i sub($interval);//从日期对象中减去1个月
echo$i.“2012年11月1日之前的月份为“$date->格式('fj,Y');
}

使用
DateTime
DateInterval
类来实现这一点

$date = new DateTime('November 1, 2012');
$interval = new DateInterval('P1M'); // A month

for($i = 1; $i <= $nrOfMonths; $i++) {
    $date->sub($interval); // Subtract 1 month from the date object
    echo $i . " month(s) prior to November 1, 2012 was " . $date->format('F j, Y');
}
$date=新日期时间('2012年11月1日');
$interval=new DateInterval('P1M');//一个月
对于($i=1;$i sub($interval);//从日期对象中减去1个月
echo$i.“2012年11月1日之前的月份为“$date->格式('fj,Y');
}

因此使用“2012年11月1日-$nrofmonths月”也可以使用“2012年11月1日-$nrofmonths月”您还可以将第二个参数传递到strotime以表示相对日期的基本时间戳。尽管
DateTime
方法似乎更合适。函数getEvents(nrofmonths,dateX=“2012年11月1日”){$stopDate=strtotime($dateX'-'.$nrOfMonths.'months');…代码的其余部分…}看起来更好。您还可以将第二个参数传递给
strtotime
,以表示相对日期的基本时间戳。尽管
DateTime
方法似乎更合适。函数GetEvents(nrOfMonths,dateX=“2012年11月1日”){$stopDate=strotime($dateX'-'.$nrOfMonths.'months');…代码的其余部分…}看起来更好