Ihsaan Posted March 19, 2013 Share Posted March 19, 2013 How would i display total income for last week in php/mySql? My table has the following columns: ID, Date, Total Quote Link to comment https://forums.phpfreaks.com/topic/275846-display-total-earnings-for-last-week/ Share on other sites More sharing options...
DaveyK Posted March 19, 2013 Share Posted March 19, 2013 What is your date? Is it a datestamp, timestamp, unix_timestamp, datetime? I suggest you look into SUM() in SQL Quote Link to comment https://forums.phpfreaks.com/topic/275846-display-total-earnings-for-last-week/#findComment-1419512 Share on other sites More sharing options...
jazzman1 Posted March 19, 2013 Share Posted March 19, 2013 The easiest way to make this is to use a mysql proper date format in the date column. SELECT `Total` FROM tbl_name WHERE DATE_SUB(CURDATE(),INTERVAL 7 DAY) <= `Date` OREDER BY `ID` DESC; Quote Link to comment https://forums.phpfreaks.com/topic/275846-display-total-earnings-for-last-week/#findComment-1419518 Share on other sites More sharing options...
DaveyK Posted March 19, 2013 Share Posted March 19, 2013 Im pretty sure the total the OP is referring to are total per day, so that wont return the totals for the entire week, but rather the totals per day. Quote Link to comment https://forums.phpfreaks.com/topic/275846-display-total-earnings-for-last-week/#findComment-1419520 Share on other sites More sharing options...
Ihsaan Posted March 19, 2013 Author Share Posted March 19, 2013 Its in the format of 2012-12-11 08:49:38 Quote Link to comment https://forums.phpfreaks.com/topic/275846-display-total-earnings-for-last-week/#findComment-1419555 Share on other sites More sharing options...
Ihsaan Posted March 20, 2013 Author Share Posted March 20, 2013 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; ?> Quote Link to comment https://forums.phpfreaks.com/topic/275846-display-total-earnings-for-last-week/#findComment-1419768 Share on other sites More sharing options...
Zane Posted March 20, 2013 Share Posted March 20, 2013 SELECT SUM(total)FROM invoicesWHERE created BETWEEN DATEADD(wk, -1, GetDate()) AND GetDate() Quote Link to comment https://forums.phpfreaks.com/topic/275846-display-total-earnings-for-last-week/#findComment-1419773 Share on other sites More sharing options...
Ihsaan Posted March 20, 2013 Author Share Posted March 20, 2013 Still showing up blank Quote Link to comment https://forums.phpfreaks.com/topic/275846-display-total-earnings-for-last-week/#findComment-1419781 Share on other sites More sharing options...
Ihsaan Posted March 20, 2013 Author Share Posted March 20, 2013 <? $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 $show_lastweek ?> Quote Link to comment https://forums.phpfreaks.com/topic/275846-display-total-earnings-for-last-week/#findComment-1419782 Share on other sites More sharing options...
Zane Posted March 20, 2013 Share Posted March 20, 2013 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 Quote Link to comment https://forums.phpfreaks.com/topic/275846-display-total-earnings-for-last-week/#findComment-1419783 Share on other sites More sharing options...
Ihsaan Posted March 20, 2013 Author Share Posted March 20, 2013 I tried it without the quotes and I get a server error 500 and when i try it with single quotes i get the same blank result Quote Link to comment https://forums.phpfreaks.com/topic/275846-display-total-earnings-for-last-week/#findComment-1419790 Share on other sites More sharing options...
DaveyK Posted March 20, 2013 Share Posted March 20, 2013 You get a 500 error because you are not using quotes? That seems awfully strange. Never heard that before. Are you sure o_O ? Quote Link to comment https://forums.phpfreaks.com/topic/275846-display-total-earnings-for-last-week/#findComment-1419793 Share on other sites More sharing options...
Zane Posted March 20, 2013 Share Posted March 20, 2013 (edited) 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>"; ?> Edited March 20, 2013 by Zane Quote Link to comment https://forums.phpfreaks.com/topic/275846-display-total-earnings-for-last-week/#findComment-1419794 Share on other sites More sharing options...
Ihsaan Posted March 20, 2013 Author Share Posted March 20, 2013 @DaveyK strange but true... Im running xampp maybe that has something to do with it... @Zane Thanks man that makes a diffierence only thing its not echoing what i want. It just echoes "1" Quote Link to comment https://forums.phpfreaks.com/topic/275846-display-total-earnings-for-last-week/#findComment-1419817 Share on other sites More sharing options...
Ihsaan Posted February 23, 2014 Author Share Posted February 23, 2014 (edited) 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 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>"; ?> Edited February 23, 2014 by Ihsaan Quote Link to comment https://forums.phpfreaks.com/topic/275846-display-total-earnings-for-last-week/#findComment-1470310 Share on other sites More sharing options...
jazzman1 Posted February 23, 2014 Share Posted February 23, 2014 (edited) 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()); Edited February 23, 2014 by jazzman1 Quote Link to comment https://forums.phpfreaks.com/topic/275846-display-total-earnings-for-last-week/#findComment-1470317 Share on other sites More sharing options...
Barand Posted February 24, 2014 Share Posted February 24, 2014 First, define "last week". Is that during the previous calendar week (and is that Sun-Sat or Mon-Sun) or the last 7 days Quote Link to comment https://forums.phpfreaks.com/topic/275846-display-total-earnings-for-last-week/#findComment-1470381 Share on other sites More sharing options...
Ihsaan Posted February 25, 2014 Author Share Posted February 25, 2014 Actually the string I ran in mySql is SELECT SUM(total) FROM invoices WHERE date_created BETWEEN DATEADD(wk, -1,'. GetDate().') AND '.GetDate and the error is FUNCTION billing_accounts.DATEADD does not exist Quote Link to comment https://forums.phpfreaks.com/topic/275846-display-total-earnings-for-last-week/#findComment-1470568 Share on other sites More sharing options...
Barand Posted February 25, 2014 Share Posted February 25, 2014 (edited) 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) Edited February 25, 2014 by Barand Quote Link to comment https://forums.phpfreaks.com/topic/275846-display-total-earnings-for-last-week/#findComment-1470574 Share on other sites More sharing options...
Ihsaan Posted February 25, 2014 Author Share Posted February 25, 2014 Thanks Barand that worked only thing is that I would like to get the total of last week's sales and not the last seven days. Any idea how to achieve this? Quote Link to comment https://forums.phpfreaks.com/topic/275846-display-total-earnings-for-last-week/#findComment-1470582 Share on other sites More sharing options...
Ihsaan Posted February 25, 2014 Author Share Posted February 25, 2014 $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 Quote Link to comment https://forums.phpfreaks.com/topic/275846-display-total-earnings-for-last-week/#findComment-1470594 Share on other sites More sharing options...
Barand Posted February 25, 2014 Share Posted February 25, 2014 Yes, answer the question when it's asked 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' "; Quote Link to comment https://forums.phpfreaks.com/topic/275846-display-total-earnings-for-last-week/#findComment-1470596 Share on other sites More sharing options...
Barand Posted February 25, 2014 Share Posted February 25, 2014 $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']); ?> Quote Link to comment https://forums.phpfreaks.com/topic/275846-display-total-earnings-for-last-week/#findComment-1470597 Share on other sites More sharing options...
Ihsaan Posted February 25, 2014 Author Share Posted February 25, 2014 Still coming up blank Quote Link to comment https://forums.phpfreaks.com/topic/275846-display-total-earnings-for-last-week/#findComment-1470608 Share on other sites More sharing options...
Barand Posted February 25, 2014 Share Posted February 25, 2014 Turn error reporting on Quote Link to comment https://forums.phpfreaks.com/topic/275846-display-total-earnings-for-last-week/#findComment-1470609 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.