fetch last months sales total value

Hi everyone,

I’m trying to pull all of the sales subtotals together from my database for the previous month, this is where I am so far but I cannot get the date format to work correctly.

Any help would be appreciated

[php]<?php

if (mysqli_connect_errno($db)) {
trigger_error('Database connection failed: ’ . mysqli_connect_error(), E_USER_ERROR);
}

$currentdate = date(‘Y-m-d’); // I know this is the current date

$sql=“SELECT SUM(subtotal) AS sales FROM fms_tbl_invoicing WHERE client_id= ‘{$_SESSION[‘client_id_of_user’]}’ AND date LIKE ‘$currentdate’%”;

while($sales = mysqli_fetch_assoc($result)) {
echo number_format(floatval($sales[‘sales’]),2);
}

?> [/php]

First thing, you may want to re-evaluate how you grab the date. Is the date column a date type? Using a query that would check the month and year would be more prosperous.

[php]
$date = new DateTime();
$dateYear = $date->format(‘Y’);
$dateMonth = $date->format(‘m’);

WHERE YEAR(date) = ? AND MONTH(date) = ?

$stmt->bind_param(“ii”, $dateYear, $dateMonth);
$stmt->execute();
[/php]

For the number formatting,
[php]number_format($num, 2);[/php]

For monetary formatting,
[php]money_format(’%.2n’, $number); // this only works in Linux env[/php]

Sponsor our Newsletter | Privacy Policy | Terms of Service