编辑代码

<?php 
function strMonthDate($month, $sourceDate = '', $type = '')
    {
        if (!$sourceDate) {
            $sourceDate = date('Y-m-d H:i:s');
        }
        // 原始日期时间戳
        $timestamp = strtotime($sourceDate);
        $sourceDay = date('d', $timestamp);

        // 获取原始日期月份第一天:无论加减,月份第一天都不会有bug
        $monthFirstDay = date('Y-m-01 H:i:s', $timestamp);

        // 月份增减
        $newDate = strtotime($month . ' month', strtotime($monthFirstDay));
        $newMonthDays = date('t', $newDate);
        if ($sourceDay > $newMonthDays) {
            $sourceDay = $newMonthDays;
        }

        if ($type == 'date') {
            return date('Y-m-' . $sourceDay, $newDate);
        } else {
            return date('Y-m-' . $sourceDay . ' H:i:s', $newDate);
        }
    }
    $a = strMonthDate(1,'2029-12-31 23:59:59');
    var_dump($a);