자유게시판

setDate와 ->modify에서 에러가나니 이 두분 없는 코드를 만들어줘

작성자 정보

  • 삼둡 작성
  • 작성일

컨텐츠 정보

본문

<?php
$servername = "your_mysql_server";
$username = "your_mysql_username";
$password = "your_mysql_password";
$dbname = "your_database_name";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
   die("Connection failed: " . $conn->connect_error);
}

// Function to calculate average cost per gram
function calculateAverageCost($totalCost, $totalQuantity) {
   if ($totalQuantity > 0) {
       return $totalCost / $totalQuantity;
   } else {
       return 0;
   }
}

// Get the current year and quarter
$currentYear = date('Y');
$currentQuarter = ceil(date('n') / 3);

// Iterate over all quarters until data is found or until we reach the first quarter of the year 1970
while ($currentQuarter > 0) {
   $startThisQuarterDate = new DateTime("$currentYear-" . (($currentQuarter - 1) * 3 + 1) . "-01");
   $endThisQuarterDate = new DateTime("$currentYear-" . ($currentQuarter * 3) . "-01");
   $endThisQuarterDate->modify('last day of this month');

 

$endThisQuarterDate = new DateTime("$currentYear-" . ($currentQuarter * 3) . "-01");
$endThisQuarterDate->modify('last day of this month');

// Find the last day of the month
$endThisQuarterDate->setDate($endThisQuarterDate->format('Y'), $endThisQuarterDate->format('m'), $endThisQuarterDate->format('t'));

// Now $endThisQuarterDate contains the last day of the quarter
 

$endThisQuarterDate = DateTime::createFromFormat('Y-m-d', "$currentYear-" . ($currentQuarter * 3) . "-01");
$endThisQuarterDate->modify('last day of this month');

// Find the last day of the month
$endThisQuarterDate->setDate($endThisQuarterDate->format('Y'), $endThisQuarterDate->format('m'), $endThisQuarterDate->format('t'));

// Now $endThisQuarterDate contains the last day of the quarter
$year = $currentYear;
$quarter = $currentQuarter;

// Calculate the last day of the quarter
$lastDayOfQuarter = mktime(0, 0, 0, $quarter * 3 + 1, 0, $year);

// Find the last day of the month
$lastDayOfQuarter = strtotime(date("Y-m-t", $lastDayOfQuarter));

$endThisQuarterDate = new DateTime(date('Y-m-d', $lastDayOfQuarter));

// Now $endThisQuarterDate contains the last day of the quarter
 

 

   $avgPrevCostPerGram = 0;

   // Get the data for the previous quarter
   $sql = "SELECT uh.herbname,
                  uh.price * uh.quantity as total_prev_cost,
                  uh.quantity as total_prev_quantity
           FROM used_herb uh
           WHERE uh.date >= '" . $startThisQuarterDate->format('Y-m-d H:i:s') . "'
               AND uh.date <= '" . $endThisQuarterDate->format('Y-m-d H:i:s') . "'";

   $result = $conn->query($sql);

   if ($result->num_rows > 0) {
       $totalPrevCost = 0;
       $totalPrevQuantity = 0;
       while ($row = $result->fetch_assoc()) {
           $totalPrevCost += $row["total_prev_cost"];
           $totalPrevQuantity += $row["total_prev_quantity"];
       }
       $avgPrevCostPerGram = calculateAverageCost($totalPrevCost, $totalPrevQuantity);

       // Update the herb table with the calculated average cost per gram for the matching herbname
       $updateSql = "UPDATE herb
                     SET cost_per_gram = $avgPrevCostPerGram
                     WHERE herbname IN (
                         SELECT DISTINCT uh.herbname
                         FROM used_herb uh
                         WHERE uh.date >= '" . $startThisQuarterDate->format('Y-m-d H:i:s') . "'
                             AND uh.date <= '" . $endThisQuarterDate->format('Y-m-d H:i:s') . "'
                     )";
       $conn->query($updateSql);

       echo "Herb table updated successfully for quarter $currentQuarter in $currentYear.";
       break;  // Break the loop once data is found and updated
   }

   // Move to the previous quarter
   $currentQuarter--;

   // If it's the first quarter of the year, consider the last quarter of the previous year
   if ($currentQuarter == 0) {
       $currentQuarter = 4;
       $currentYear--;
   }
}

$conn->close();
?>
 

관련자료

댓글 0
등록된 댓글이 없습니다.

인기검색어


최근글


새댓글