解决php日期 加减问题
希望大神能给一个好用点的 想要的功能是这样的
一个日期加上年月日 等于另外一个日期 不用 date("Y-m-d",strtotime("+1 day")) 因为 日期过大 会变成1970 存在这个bug
所以只能用DateTime函数
比如日期 2019-10-01 加上 00_01_00 ( 00_01_00 表示 0年 1个月 0天 ) 要等于 2019-10-31
2019-03-03 加上 00_02_00 要等于 2019-05-02
2019-03-03 加上 01_02_00 要等于 2020-05-02
第一后面的日期加上年月日后 要比前面的日期 少一天
第二但是碰到2 月的要能自动变
例如 2019-01-01 加上 00_02_00 要等于 2019-02-28
我用了下面的自定义函数 出现了问题 当 2019-01-01 加上 01_02_00 要等于 2020-02-29 但是变成了 2020-02-28 请问怎么办,有大神能搞定了可以私信我,有偿服务。
$xq='05_06_00';
$qr='2019-03-03';
echo qr_add_xq($xq,$qr,$zr);
function qr_add_xq($xq,$qr,$zr){
if(preg_match("/^\d{2}_\d{2}_\d{2}/",$xq) and preg_match("/^\d{4}-\d{2}-\d{2}/",$qr) and preg_match("/^\d{4}-\d{2}-\d{2}/",$zr) and strlen($xq)==8 and strlen($qr)==10 and strlen($zr)==10 ){
$time_explode = explode('_',$xq);
$time_y=$time_explode[0];
$time_m=$time_explode[1];
$time_d=$time_explode[2];
$c1_zr = new DateTime($qr);
$c1_zr->modify( '+'.$time_d. 'day');
$c1_zr=$c1_zr->format('Y-m-d');
$dt = new DateTime($c1_zr);
$day = $dt->format('j');
$dt->modify('first day of'. '+'.$time_m. 'month');
$dt->modify('+' . (min($day, $dt->format('t'))-2) . ' days');
$c2_zr= $dt->format('Y-m-d');
$c3_zr = new DateTime($c2_zr);
$c3_zr->modify('+'.$time_y .'year');
$c_zr=$c3_zr->format('Y-m-d');
return $c_zr;
}
}