对于第一次运行,我需要使用最后一个 MySQL date 之前六个月的日期$row_recent[0]
。对于 1 之后的所有运行,我使用先前的变量来存储先前的日期,然后将日期减少 6 个月。我已经确认第一个 if 测试产生了预期的结果。(MySQL 日期 - 6 个月)。但是,第二个 if 测试$startdate6m
由于$previous_6m
未初始化而输出为 PHP 默认值。知道为什么它不会识别$previous_6m = $initial6m
吗?
<?php
$run_number = 0;
while ($run_number < 15) {
$run_number++;
if($run_number == 1){
if ($month <= 06){
$year6m = date("Y", strtotime($row_recent[0]))-1;
$month6m = str_pad((12-(6-date("m", strtotime($row_recent[0])))), 2, "0", STR_PAD_LEFT);
$startdate6m = "'".$year6m."-".$month6m."-01'";
$end_date = $startdate6m;
$initial6m = $startdate6m;
} else{
$year6m = date("Y", strtotime($row_recent[0]));
$month6m = str_pad(date("m", strtotime($row_recent[0]))-6, 2, "0", STR_PAD_LEFT);
$startdate6m = "'".$year6m."-".$month6m."-01'";
$end_date = $startdate6m;
$initial6m = $startdate6m;
}
}
$previous_6m = $initial6m;
if($run_number > 1){
# 6 Month
# Decrement date by 6 months
$month6m = date("m", strtotime($previous_6m));
if ($month6m <= 06){
$year6m = date("Y", strtotime($previous_6m))-1;
$month6m = str_pad((12-(6-date("m", strtotime($previous_6m)))), 2, "0", STR_PAD_LEFT);
$startdate6m = "'".$year6m."-".$month6m."-01'";
$end_date = $startdate6m;
} else{
$year6m = date("Y", strtotime($previous_6m));
$month6m = str_pad(date("m", strtotime($previous_6m))-6, 2, "0", STR_PAD_LEFT);
$startdate6m = "'".$year6m."-".$month6m."-01'";
$end_date = $startdate6m;
}
}
$previous_6m = $startdate6m;
}
?>