Jump to content

Display total earnings for last week


Ihsaan

Recommended Posts

This is what I have at the moment but nothing shows up.

 

<?php $get_lastweek = mysql_query("SELECT SUM(total) FROM invoices WHERE created >= curdate() - INTERVAL DAYOFWEEK(curdate())+6 DAY AND created < curdate() - INTERVAL DAYOFWEEK(curdate())-1 DAY");

$show_lastweek = mysql_fetch_array("$get_lastweek"); ?>

<?php echo $show_lastweek; ?>

It may not be the problem, PHP is pretty forgiving, but why do you have $get_lastweek inside double quotes?  I mean, double quotes WILL parse the variable, but I've never ever seen anyone pass a resource value that way.  Try it without the quotes, just so I don't go crazy...

 

Also, the use of short tags .... ?> suggests to me that you are using an old version of PHP

Try this

<?php
$get_lastweek = mysql_query("SELECT SUM(total) FROM invoices WHERE date_created BETWEEN DATEADD(wk, -1, GetDate()) AND GetDate()");
$show_lastweek = mysql_fetch_array($get_lastweek); ?>


<?php echo "<pre>" . print_r($show_lastweek) . "</pre>"; ?>
 

 

 

  • 11 months later...

I tried running the following as suggested as a SQL query directly in mySql and I get the following error

 

FUNCTION billing_accounts.DATEADD does not exist

 

  On 3/20/2013 at 11:49 AM, Zane said:

Try this

<?php
$get_lastweek = mysql_query("SELECT SUM(total) FROM invoices WHERE date_created BETWEEN DATEADD(wk, -1, GetDate()) AND GetDate()");
$show_lastweek = mysql_fetch_array($get_lastweek); ?>


<?php echo "<pre>" . print_r($show_lastweek) . "</pre>"; ?>
 

If you want to use php functions inside sql string, then the syntax should be different.

 

Try,

$get_lastweek = mysql_query('SELECT SUM(total) FROM invoices WHERE date_created BETWEEN DATEADD(wk, -1,'. GetDate().') AND '.GetDate());

I'd use

SELECT SUM(total) as total
FROM invoices 
WHERE date_created > CURDATE() - INTERVAL 7 DAY

but the function you were looking for is DATE_ADD() or DATE_SUB() (which is what you want in this instance)

$get_lastweek = mysql_query("SELECT SUM(total) as total FROM invoices WHERE date_created > CURDATE() - INTERVAL 7 DAY");

$show_lastweek = mysql_fetch_array("$get_lastweek");

 

<?php echo ($show_lastweek); ?>

 

When I insert the above into my code it returns nothing

Yes, answer the question when it's asked

 

  On 2/24/2014 at 10:14 AM, Barand said:

 

First, define "last week".

 

Is that

  • during the previous calendar week (and is that Sun-Sat or Mon-Sun)
  • or the last 7 days

 

 

Then

$dt = new DateTime('last saturday');
$saturday = $dt->format('Y-m-d');
$sql = "SELECT SUM(total) as total
    FROM invoices 
    WHERE date_created BETWEEN '$saturday' - INTERVAL 6 DAY AND '$saturday' ";
  On 2/25/2014 at 12:36 PM, Ihsaan said:

 

$get_lastweek = mysql_query("SELECT SUM(total) as total FROM invoices WHERE date_created > CURDATE() - INTERVAL 7 DAY");
$show_lastweek = mysql_fetch_array("$get_lastweek");
 
<?php echo ($show_lastweek); ?>
 
When I insert the above into my code it returns nothing

 

 

should be

<?php echo ($show_lastweek['total']); ?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.