php 试着用月末的问题来计算月收入

vnjpjtjt  于 2023-06-21  发布在  PHP
关注(0)|答案(1)|浏览(123)

我试图计算每月收入,但我希望它是自动的,我也希望它能考虑到月底的问题。
我希望它从12日计算到11日,但我希望它也包括11日的任何收入,这样每个月11日就不会被遗漏。
我可以让它计算,但它只计算12号到10号的收入,而不是11号。
我试图让它自动化,这样当月份发生变化时,它仍然是准确的,不管这个月是28天,30天还是31天,但我找不到一种正确的方法来解决所有这些问题。
我这样做了3个月,所以3段代码在另一个下面,但似乎只有第二块代码(计算12日,5月-11日,6月)不包括11日的收入(尽管这可能对当月也是一样的,我不知道,直到下个月),
编辑-它只是一个简单的毛利太,所以它只是乘以'价格'列的'数量'列foreach行,然后总结总数。

编辑-我手动计算了第三个月的毛利(4月12日至5月11日),但由于5月11日没有收入,所以它是准确的,所以我相信唯一的问题是它不包括每个月11日的任何收入

这是我的
任何帮助将非常感谢!

当月:

<h5 style="color:black;">Monthly Earnings <small style="font-size:12px;">(gross profit)</small></h5>

    <br>

    <?php
// Set the timezone to the appropriate timezone
date_default_timezone_set('Europe/London');

// Get the current date
$current_date = date('Y-m-d');

// Determine the start and end dates for the current period
$current_month = date('n', strtotime($current_date));
$current_year = date('Y', strtotime($current_date));

// Adjust the start date to be the 12th of the current month
$start_date_1st = date('Y-m-d', strtotime("$current_year-$current_month-12"));

// Determine the number of days in the current month
$days_in_month = date('t', strtotime($current_date));

// Adjust the end date based on the number of days in the current month
if (date('j', strtotime($current_date)) < 12) {
// If the current date is before the 12th, set the end date to the 11th of the current month
$end_date_1st = date('Y-m-d', strtotime("11-$current_month-$current_year"));
} else {
// If the current date is on or after the 12th, set the end date to the 11th of the next month
if ($current_month == 12) {
    $next_month = 1;
    $next_year = $current_year + 1;
} else {
    $next_month = $current_month + 1;
    $next_year = $current_year;
}
$end_date_1st = date('Y-m-d', strtotime("11-$next_month-$next_year"));
}

// Format the start and end dates as "Month, day"
$start_date_formatted_1st = date('F, jS', strtotime($start_date_1st));
$end_date_formatted_1st = date('F, jS', strtotime($end_date_1st));
$today_formatted = date('F, jS', strtotime($current_date));

if ($today_formatted == $start_date_formatted_1st) {
    $start_date_formatted_1st = "Today";
}

// Query the database for orders within the current period
$howMuch = $dbh->prepare("SELECT * FROM `orders` WHERE `date` BETWEEN :start_date_1st AND :end_date_1st ORDER BY `id`");
$howMuch->bindParam(':start_date_1st', $start_date_1st);
$howMuch->bindParam(':end_date_1st', $end_date_1st);
$howMuch->execute();
$seeSold = $howMuch->fetchAll();

// Calculate the total profit for the current period
$Money1st = 0;
foreach ($seeSold as $firstResult) {
    $secondResult = $firstResult['price'] * $firstResult['quantity'];
    $Money1st += $secondResult;
}
?>

<h6 style="color:black;margin-top:0;"><?php echo $start_date_formatted_1st . ' - ' . $end_date_formatted_1st;?></h6>

<?php if (!empty($seeSold)):?>
<h3 style="color:black;">£<?php echo number_format(round($Money1st, 2), 2);?></h3>
<?php else: ?>
<h3 style="color:black;">£0.00</h3>
<?php endif ?>

上一个月:

<?php
// Set the timezone to the appropriate timezone
date_default_timezone_set('Europe/London');

// Get the current date
$current_date = new DateTime();

// Determine the start and end dates for the previous period
$current_month = $current_date->format('n');
$current_year = $current_date->format('Y');

// Calculate the start and end dates for the previous period
if ($current_month == 1) {
    $start_date_2nd = new DateTime("12-12-" . ($current_year - 1));
    $end_date_2nd = new DateTime("11-$current_month-$current_year");
} else {
    $start_date_2nd = new DateTime("12-" . ($current_month - 1) . "-$current_year");
    $end_date_2nd = new DateTime("11-$current_month-$current_year");
}

// Format the start and end dates as "F jS"
$start_date_formatted_2nd = $start_date_2nd->format('F jS');
$end_date_formatted_2nd = $end_date_2nd->format('F jS');

// Query the database for orders within the previous period
$howMuchSecond = $dbh->prepare("SELECT * FROM `orders` WHERE `date` BETWEEN :start_date_2nd AND :end_date_2nd ORDER BY `id`");
$howMuchSecond->bindParam(':start_date_2nd', $start_date_2nd->format('Y-m-d'));
$howMuchSecond->bindParam(':end_date_2nd', $end_date_2nd->format('Y-m-d'));
$howMuchSecond->execute();
$secondMonthAmount = $howMuchSecond->fetchAll();

// Calculate the total profit for the previous period
$moneyStart = 0;
foreach ($secondMonthAmount as $secondMonthResult) {
    $secondPartResult = $secondMonthResult['price'] * $secondMonthResult['quantity'];
    $moneyStart += $secondPartResult;
}
?>

<h6 style="color:black;margin-top:0;"><?php echo $start_date_formatted_2nd . ' - ' . $end_date_formatted_2nd;?></h6>

<?php if ($moneyStart > 0):?>
<h3 style="color:black;">£<?php echo number_format(round($moneyStart, 2), 2);?></h3>
<?php else: ?>
<h3 style="color:black;">£0.00</h3>
<?php endif ?>

3月返回:

<?php
// Set the timezone to the appropriate timezone
date_default_timezone_set('Europe/London');

// Get the current date
$current_date = date('Y-m-d');

// Determine the start and end dates for the previous period
$current_month = date('n', strtotime($current_date));
$current_year = date('Y', strtotime($current_date));
if (date('d', strtotime($current_date)) <= 11) {
    $start_date_3rd = date('Y-m-d', strtotime("12-$current_month-$current_year -3 months"));
    $end_date_3rd = date('Y-m-d', strtotime("11-$current_month-$current_year -2 months"));
} else {
    $start_date_3rd = date('Y-m-d', strtotime("12-$current_month-$current_year -2 months"));
    $end_date_3rd = date('Y-m-d', strtotime("11-$current_month-$current_year -1 month"));
}

// Format the start and end dates as "Month, day"
$start_date_formatted_3rd = date('F, jS', strtotime($start_date_3rd));
$end_date_formatted_3rd = date('F, jS', strtotime($end_date_3rd));

// Query the database for orders within the previous period
$month3rdback = $dbh->prepare("SELECT * FROM `orders` WHERE `date` BETWEEN :start_date_3rd AND :end_date_3rd ORDER BY `id`");
$month3rdback->bindParam(':start_date_3rd', $start_date_3rd);
$month3rdback->bindParam(':end_date_3rd', $end_date_3rd);
$month3rdback->execute();
$see3rdMonth = $month3rdback->fetchAll();

// Calculate the total profit for the previous period
$readySet = 0;
foreach ($see3rdMonth as $month3rdbackResult) {
    $month3rdbackSecondRes = $month3rdbackResult['price'] * $month3rdbackResult['quantity'];
    $readySet += $month3rdbackSecondRes;
}
?>

<h6 style="color:black;margin-top:0;"><?php echo $start_date_formatted_3rd . ' - ' . $end_date_formatted_3rd;?></h6>

<?php if (!empty($see3rdMonth)): ?>
<h3 style="color:black;">£<?php echo number_format(round($readySet, 2), 2);?></h3>
<?php else: ?>
<h3 style="color:black;">£0.00</h3>
<?php endif ?>
cuxqih21

cuxqih211#

我知道你通过CAST ing解决了这个问题,但我想我会把这个解决方案放在这里,因为你可以用SQL查询替换你的PHP代码来查找总和:

SELECT 
    COUNT(id) as number_of_orders, 
    ROUND(SUM(price * quantity),2) as gross_profit 
FROM orders 
    WHERE date BETWEEN CAST(:start) AND CAST(:end);

然后将其丢弃在一个函数中以实现可重用性:

function getGrossProfit($dbh, $startDate, $endDate)
{
    $query = $dbh->prepare("SELECT COUNT(id) as number_of_orders, ROUND(SUM(price * quantity),2) as gross_profit FROM orders WHERE date BETWEEN CAST(:start) AND CAST(:end)");
    $query->bindParam(':start', $startDate);
    $query->bindParam(':end', $endDate);
    $query->execute();
    return $query->fetchAll();
}

相关问题