gpt4 book ai didi

php - 如何使用php计算两个日期之间的金额

转载 作者:行者123 更新时间:2023-11-29 19:18:47 25 4
gpt4 key购买 nike

<?php
if(isset($_SESSION['role'])) {
$role=$_SESSION['role'];
}

if(isset($_POST["fdate"])) {
$fdate=$_POST["fdate"];
$_SESSION['fdate']=$fdate;
} else if(isset($_SESSION['fdate'])) {
$fdate=$_SESSION['fdate'];
}
$todate='';
if(isset($_POST["todate"])) {
$todate=$_POST["todate"];
$_SESSION['todate']=$todate;
} else if(isset($_SESSION['todate'])) {
$todate=$_SESSION['todate'];
}
//$_SESSION["sql"]=$sql;
$begin = new DateTime($fdate. " 00:00:00");
$end = new DateTime($todate. " 23:59:59");
$interval = DateInterval::createFromDateString('1 day');
$period = new DatePeriod($begin, $interval, $end);
$_SESSION["fdate"]=$fdate;
$_SESSION["todate"]=$todate;
foreach ( $period as $dt ) {
$new=$dt->format("y-m-d");
$profit_detail=mysql_query("select * from project_detail where date like '%".$new."%' group by project_id ");
$old_value='0';
$new_value='0';
$value='0';
$balance='0';
while($row=mysql_fetch_array($profit_detail)) {
?>
<tr bgcolor="#FFFFFF">
<td><?php echo $row["date"];?></td>
<td><?php echo $row["companyname"];?></td>
<td><?php echo $row["project_name"];?></td>
<td><?php echo $row["amount_given"];
$old_value=$old_value+$row["amount_given"];?></td>
<td><?php echo $row["total_amount"]; $new_value=$new_value+$row["total_amount"];?></td>
<td><?php echo $value=$value+$row["blank"]?></td>
</tr>
<?php
}
}
$balance=$old_value-$new_value;
?>
<tr bgcolor="#FFFFFF">
<td>Total</td>
<td></td>
<td></td>
<td><?php echo $old_value;?></td>
<td><?php echo $new_value;?></td>
<td><?php echo $balance;?></td>
</tr>
</tbody>
</table>
</div>

我想计算两个选定日期之间的总金额,但问题是当我选择日期并仅显示结果金额总和时当前日期金额总和我的sql查询或日期问题中的问题在哪里,如果日期问题比此处显示的金额和详细信息的所有记录我的输出链接https://ibb.co/k9yj8v

最佳答案

您正在循环中重新初始化值

//Add this before foreach
$old_value='0';
$new_value='0';
$value='0';
$balance='0';
foreach ( $period as $dt )
{
$new=$dt->format("y-m-d");
$profit_detail=mysql_query("select * from project_detail where date like '%".$new."%' group by project_id ");


while($row=mysql_fetch_array($profit_detail))
{
?>
<tr bgcolor="#FFFFFF">
<td><?php echo $row["date"];?></td>
<td><?php echo $row["companyname"];?></td>
<td><?php echo $row["project_name"];?></td>
<td><?php echo $row["amount_given"];

$old_value=$old_value+$row["amount_given"];?></td>

<td><?php echo $row["total_amount"]; $new_value=$new_value+$row["total_amount"];?></td>

<td><?php echo $value=$value+$row["blank"]?></td>

</tr>
<?php

}
}
$balance=$old_value-$new_value;

?>
<tr bgcolor="#FFFFFF">
<td>Total</td>
<td></td>
<td></td>
<td><?php echo $old_value;?></td>
<td><?php echo $new_value;?></td>
<td><?php echo $balance;?></td>
</tr>
</tbody>
</table>
</div>

关于php - 如何使用php计算两个日期之间的金额,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42547983/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com