计算-1个月时的Php天数错误

I surprised when i getting the issue in PHP date calculation.

$add = '- 30 days';
echo date('Y-m-01', strtotime($add)); // result is 2017-02-01 which is correct as need

but in

$add = '-1 month';
echo date('Y-m-01', strtotime($add)); // result is 2017-03-01 which is in correct

please help me in this how could i resolve this issue.

You can have look at : http://php.net/manual/en/function.strtotime.php (explanations and comments)

Note:

Using this function for mathematical operations is not advisable. It is better to use DateTime::add() and DateTime::sub() in PHP 5.3 and later, or DateTime::modify() in PHP 5.2.

Why not to take advantage of DateTime relative formats and simply use:

$date = new DateTime('first day of last month');
echo $date->format('Y-m-d');

In your case you Calculating February month. In February month have only 28 days right?

As noted in several blogs, strtotime() solves the "-1 month" ("previos month") issue on days that do not exist in the subsequent month differently than other implementations like for example MySQL.

<?php
echo date( "Y-m-d", strtotime( "2017-01-31 -1 month" ) ); // PHP:  2016-12-31
echo date( "Y-m-d", strtotime( "2017-01-31 -2 month" ) ); // PHP:  2016-12-01
?>

If you calculate like this you get correct:

$add = '- 28 days';
echo date('Y-m-01', strtotime($add))."<br/>"; 

$add = '-1 month';
echo date('Y-m-01', strtotime($add))."<br/>";