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 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 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; 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. 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 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; ?> 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() 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 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 ?> 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 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 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 ? 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 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>"; ?> 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" 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 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>"; ?> 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 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()); 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 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 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 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) 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? 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 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 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' "; 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 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']); ?> 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 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 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
Archived
This topic is now archived and is closed to further replies.