Solved

Retrieve rows based on month/year from mysql

Posted on 2012-04-10
4
550 Views
Last Modified: 2012-04-11
Hello,

I'm trying to construct a query to retrieve all the call costs incurred during a particular month passed from a post (post will look like: mydomain.com/script.php&timeperiod=2012-01 for instance) .

The timestamp for each call that has come in during the month is stored in the DB like: 2012-01-17 10:36:21

Not sure how to construct the 'WHERE' part to just look at the month/year and not the day or time?

Here is what I have currently.

$timeperiod = $_REQUEST['timeperiod];

$result = mysql_query("SELECT SUM(CallCost) FROM callers WHERE account_id = '$account_id' AND CallDate = '$timeperiod'");
            while ($row = mysql_fetch_assoc($result)) {
            $totalcost = $row['SUM(CallCost)'];
            $totalcost = sprintf("%01.2f", $totalcost);
      }

So with the above, I'd be trying to retrieve all the call costs that occurred in the month of January 2012 only and totalling them up.

Thanks as always,
TJ
0
Comment
Question by:tjyoung
  • 2
4 Comments
 
LVL 6

Expert Comment

by:yawkey13
ID: 37830574
Your query will be:
SELECT SUM(CallCost) FROM callers WHERE account_id = '$account_id' AND MONTH(CallDate) = 'right($timeperiod,2)' AND YEAR(CallDate) = 'left($timeperiod,4)'

Here are the right and left functions:
function right($value, $count){
    return substr($value, ($count*-1));
}
 
function left($string, $count){
    return substr($string, 0, $count);
}

The query is solid, but check my PHP.  It is not my strong point.
0
 
LVL 109

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 37830675
Please read this article:
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_201-Handling-date-and-time-in-PHP-and-MySQL.html

You can get the first day of a month with something like this:
URL: ...&timeperiod=2012-01
$alphaperiod = date('c', strtotime($_GET['timeperiod']  . '-01'));

And the last day of a month with something like this:
$omegaperiod = date('c', strtotime($_GET['timeperiod'] . '-t'));

Then you can use these values in the WHERE clause like this:
WHERE CallDate BETWEEN '$alphaperiod' AND '$omegaperiod'
0
 
LVL 1

Author Closing Comment

by:tjyoung
ID: 37830741
Wish I read that couple months ago :)
Thanks.
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 37831868
Thanks for the points!  The way PHP functions date() and strtotime() play together is almost magical.
0

Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question