简体   繁体   中英

Insert records for the rest of the year starting on current week

I am trying to insert the same record for the rest of the year.

Since we are currently in week 13 of 2021, there are a total of 40 weeks remaining (including week 13).

Using the following code, I can get the current year and week:

define('NL', "\n");

$ddate = date("Y/m/d");
$date = new DateTime($ddate);
$currentyear = $date->format("Y");
$week = $date->format("W");

echo "Current Year: $currentyear", NL;
echo "Weeknumber: $week", NL;

The above will output the following:

Current Year: 2021
Weeknumber: 13

Here is my attempt using a WHILE loop to create INSERT queries with different week numbers:

$year  = $currentyear;

$firstDayOfYear = mktime(0, 0, 0, 1, 1, $year);
$nextSunday     = strtotime('monday', $firstDayOfYear);

while (date('Y', $nextSunday) == $year) {
  $insert = "INSERT INTO history (`WEEK`, `YEAR`, `ADDDATE`, `USERNAME`) VALUES ($nextSunday, $year', NOW(), 'jbeasley')";
  
  echo $insert, NL;

  $nextSunday = strtotime('+1 week', $nextSunday);
}

Here is how the output looks:

 INSERT INTO history (`WEEK`, `YEAR`, `ADDDATE`, `USERNAME`) VALUES (1, '2021', NOW(), 'jbeasley')
 INSERT INTO history (`WEEK`, `YEAR`, `ADDDATE`, `USERNAME`) VALUES (2, '2021', NOW(), 'jbeasley')
 INSERT INTO history (`WEEK`, `YEAR`, `ADDDATE`, `USERNAME`) VALUES (3, '2021', NOW(), 'jbeasley')
 // and so on up to 52 (there are 52 weeks in 2021)

This is working for the most part, but it's also where I got stuck.

I'm not sure how to get the INSERTs to start on the current week, disregarding all previous weeks. In this case, since we are currently in week 13, the first INSERT should start on week 13 followed by the remaining weeks, as follows:

INSERT INTO history (`WEEK`, `YEAR`, `ADDDATE`, `USERNAME`) VALUES (13, '2021', NOW(), 'jbeasley')
INSERT INTO history (`WEEK`, `YEAR`, `ADDDATE`, `USERNAME`) VALUES (14, '2021', NOW(), 'jbeasley')
INSERT INTO history (`WEEK`, `YEAR`, `ADDDATE`, `USERNAME`) VALUES (15, '2021', NOW(), 'jbeasley')
// and so on until the final week

Using the suggestion from El_Vanja, I updated the WHILE loop to include an IF/ELSE to check if the current week is greater than the:

while (date('Y', $nextSunday) == $year) {
  if($week > date('W', $nextSunday)){
    echo "skip this week " . date('W', $nextSunday), NL;
    $nextSunday = strtotime('+1 week', $nextSunday);
  }
  else{
    $insert = "INSERT INTO history (`WEEK`, `YEAR`, `ADDDATE`, `USERNAME`) VALUES ($nextSunday, $year, NOW(), 'jbeasley')";

    echo $insert, NL;

    echo date('W', $nextSunday), NL;
    $nextSunday = strtotime('+1 week', $nextSunday);
  }
}

Using the above, the loop will skip the past weeks and only start creating the query on the current week.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM