Jump to content

Select total additions in this month


Mutley

Recommended Posts

I'm trying to do a donate script that selects the total donation amount made in the current month.

 

<?php

$result = mysql_query("SELECT SUM(amount), dateline FROM forum_paymenttransaction");
while($row = mysql_fetch_array( $result ))
{
	$amount = $row['amount'];
}

?>

 

 

The above gives the total amount of money and selects the date field of when the donation was made (which is a unix timestamp) the part I'm stuck at, is how to only show the total donation for THIS present month.

 

Any ideas?

 

Thanks in advance.

 

Link to comment
https://forums.phpfreaks.com/topic/101775-select-total-additions-in-this-month/
Share on other sites

Use this (note - not tested so there may be some typos):

 

$current_date = time();
$current_month = date("m", $current_date);
$current_year = date("Y", $current_date);
switch($current_month)
{
    case 1:
    case 3:
    case 5:
    case 7:
    case 8:
    case 10:
    case 12:
       $number_of_days = 31;
       break;
    case 4:
    case 6:
    case 9:
    case 11:
        $number_of_days = 30;
    case 2:
        $number_of_days = (date("L", $current_date) == 0) ? 28 : 29;
        break;
}
$start_of_month = mktime(0, 0, 0, $current_month, 1, $current_year);
$end_of_month = mktime(23, 59, 59, $current_month, $number_of_days, $current_year);
$query = "SELECT SUM(amount), dateline FROM forum_paymenttransaction WHERE date >= {$start_of_month} AND date <= {$end_of_month}";

Thanks a lot Haku, I tweaked it to echo the result but get an error (I think your code is correct though, just bad SQL?):

 

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in [path]/modules/donations.php on line 30

 

<?php
$current_date = time();
$current_month = date("m", $current_date);
$current_year = date("Y", $current_date);
switch($current_month)
{
	case 1:
	case 3:
	case 5:
	case 7:
	case 8:
	case 10:
	case 12:
	   $number_of_days = 31;
	   break;
	case 4:
	case 6:
	case 9:
	case 11:
		$number_of_days = 30;
	case 2:
		$number_of_days = (date("L", $current_date) == 0) ? 28 : 29;
		break;
}
$start_of_month = mktime(0, 0, 0, $current_month, 1, $current_year);
$end_of_month = mktime(23, 59, 59, $current_month, $number_of_days, $current_year);
	$result = mysql_query("SELECT SUM(amount), dateline FROM forum_paymenttransaction WHERE date >= {$start_of_month} AND date <= {$end_of_month}");
	while($row = mysql_fetch_array( $result ))
	{
		$display = $row['amount'];
		echo $display;
	}
?>

 

I kind of thought that may be illegal, but I'd never tried it. You can't do what you want in 1 query. Use this:

 

$result 1= mysql_query("SELECT SUM(amount) FROM forum_paymenttransaction WHERE date >= {$start_of_month} AND date <= {$end_of_month}") or die(mysql_error());
$result2 = mysql_query("SELECT dateline FROM forum_paymenttransaction WHERE dateline >= {$start_of_month} AND dateline <= {$end_of_month}") or die(mysql_error());

Try this query (and forget that horrendous php code to come up with a start and end unix timestamp) -

 

$result = mysql_query("SELECT SUM(amount), dateline, FROM_UNIXTIME(dateline) as stddate
FROM forum_paymenttransaction
WHERE MONTH(stddate) = MONTH(CURDATE()) AND YEAR(stddate) = YEAR(CURDATE()) GROUP BY stddate");

Try this query (and forget that horrendous php code to come up with a start and end unix timestamp) -

 

$result = mysql_query("SELECT SUM(amount), dateline, FROM_UNIXTIME(dateline) as stddate
FROM forum_paymenttransaction
WHERE MONTH(stddate) = MONTH(CURDATE()) AND YEAR(stddate) = YEAR(CURDATE()) GROUP BY stddate");

 

Thanks, nice and compact, although:

 

Unknown column 'stddate' in 'where clause'

Like the error states, it does not like the alias name in the WHERE clause.  ;D

 

Do this instead (tested this time to make sure the syntax works) -

 

$query = "SELECT SUM(amount), dateline FROM forum_paymenttransaction
WHERE MONTH(FROM_UNIXTIME(dateline)) = MONTH(CURDATE()) AND
YEAR(FROM_UNIXTIME(dateline)) = YEAR(CURDATE()) GROUP BY MONTH(FROM_UNIXTIME(dateline))";

Hmm, I can't get the die(mysql_error()) to work but I have an error still:

 

 

$result = "SELECT SUM(amount), dateline FROM forum_paymenttransaction
WHERE MONTH(FROM_UNIXTIME(dateline)) = MONTH(CURDATE()) AND
YEAR(FROM_UNIXTIME(dateline)) = YEAR(CURDATE()) GROUP BY MONTH(FROM_UNIXTIME(dateline))" or die(mysql_error());
while($row = mysql_fetch_array( $result ))
	{
		$display = $row['amount'];
		echo $display;
	}

 

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in [path]/modules/donations.php on line 7

 

Thanks so far! :)

Okay, the SQL works and displays the correct amount in the database but the script displays "Resource id #29", no errors, so it must be working.

 

Can't understand why it isn't displaying the correct amount, no idea what "Resource id #29" means?

 

 

$query = "SELECT SUM(amount), dateline FROM forum_paymenttransaction
WHERE MONTH(FROM_UNIXTIME(dateline)) = MONTH(CURDATE()) AND
YEAR(FROM_UNIXTIME(dateline)) = YEAR(CURDATE()) GROUP BY MONTH(FROM_UNIXTIME(dateline))" or die(mysql_error());

$result = mysql_query($query) or die("Query failed: " . mysql_error());
echo $result;

Thanks Andy, although the Resource message has gone it is just blank now, no errors or any text.

 

 

<?php
$query = "SELECT SUM(amount), dateline FROM forum_paymenttransaction
WHERE MONTH(FROM_UNIXTIME(dateline)) = MONTH(CURDATE()) AND
YEAR(FROM_UNIXTIME(dateline)) = YEAR(CURDATE()) GROUP BY MONTH(FROM_UNIXTIME(dateline))" or die(mysql_error());

$result = mysql_query($query) or die("Query failed: " . mysql_error());
$row = mysql_fetch_array($result);
echo $row['amount'];
?>

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.