Solved

Retrieve rows based on month/year from mysql

Posted on 2012-04-10
4
517 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
Comment Utility
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 108

Accepted Solution

by:
Ray Paseur earned 500 total points
Comment Utility
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
Comment Utility
Wish I read that couple months ago :)
Thanks.
0
 
LVL 108

Expert Comment

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

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

743 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now