Search code examples
phpdate-range

How can i add all results from a field gotten with date range in php?


I queried my table to get data from the date range BETWEEN '$dat' AND '$to'

now, i have gotten like 3 rows that contains that range..... in that data, there is a field name "cost".... how can i add all the values in costs for the rows that are displayed?

//range
$dat=2021-01-06;
$to=2021-01-27;


S/N     Class Date    Class Name      Cost
1       2021-01-27    PP 2011         $200
2       2021-01-09    Excel 2016      $200
3       2021-01-06    PP 2011         $200
            
Total = $200

i want the total to be $600 i.e summing up the result

after getting the date range.

this is what i tried

$total =0;
<h5>Total = <?php echo '$'.($total += $cost); ?></h5>

Solution

  • try this if you want to do this in PHP after getting the results from mysql

    array_sum(array_column($your_mysql_results_array, 'cost'));