编辑代码

<?php 

function getAnnualReportQueryTimeOption($currentYear = null, int $currentMonth = null)
{
    $currentYear = empty($currentYear) ? date('Y') : $currentYear;
    $currentMonth = $currentMonth ? (int)$currentMonth : (int)date('m');
    echo $currentYear . '-' .date('Y');
    $startMonth = 1;    //  默认定义为最小1,则表示当前日期为今年下半年之内,应该计算今年上半年的数据,因此生成的最小月份为1
    $endMonth = 6;      //  默认定义为最大12,则表示当前日期为今年下半年之内,应该计算今年上半年的数据,因此生成的最大月份为6
    if($currentMonth<=6){     //  当前日期为今年上半年之内,应该计算去年下半年和去年一整年的数据
        $needCreateMaxMonth = 6;
        //$currentYear = date('Y', strtotime(date('Y-m-d', strtotime($currentYear.'-01-01 00:00:00')) . ' -1 day'));
        $currentYear = date('Y', strtotime(date('Y-m-d', strtotime($currentYear.'-01-01 00:00:00')) . ' -1 day'));
        $startMonth = 7;    //  当前日期为今年下半年之内,应该计算今年上半年的数据,因此生成的最小月份为1
        $endMonth = 12;     //  当前日期为今年下半年之内,应该计算今年上半年的数据,因此生成的最大月份为6
    }
    //  开始生成数据
    $retBackData = [];
    for ($i = $startMonth; $i <= $endMonth; $i++) {
        $retBackData[] = $currentYear.'-'.@str_pad((string)$i, 2, '0', STR_PAD_LEFT);
    }
    return $retBackData;
}

$currentYear = 2022;
print_r(getAnnualReportQueryTimeOption('2022', 2));



/*
$currentYear = '2022';
$cu =date('Y-m-d', strtotime($currentYear.'-01-01 00:00:00'));

echo date('Y-m-d', strtotime($currentYear.'-01-01 00:00:00'));
echo '<br/>';
echo date('Y', strtotime($cu. ' -1 day'));
echo '<br/>';
echo date('Y', strtotime(date('Y-m-d', strtotime($currentYear.'-01-01 00:00:00')) . ' -1 day'));

*/