Results 1 to 5 of 5

Thread: [RESOLVED] get count of entries between dates

  1. #1

    Thread Starter
    Hyperactive Member half flung pie's Avatar
    Join Date
    Jun 2005
    Location
    South Carolina, USA
    Posts
    317

    Resolved [RESOLVED] get count of entries between dates

    I'm not sure if this is a PHP or SQL question, as they sort of go hand-in-hand here.

    I've got a mysql table named closedtickets with many entries in it. One of the columns is called "closed" and contains the unix timestamp (reported by PHP's time() function at the time of creation)

    I'm trying to create some script that returns something like "15 records closed in the past 10 days."

    I guess I need something to the effect of
    Code:
    $backthen = mktime(0,0,0,date("m"),date("d")-10,date("Y"));
    and then an SQL query of something like
    Code:
    SELECT COUNT(closed) FROM closedtickets WHERE closed>$backthen
    I'm not really familiar with the syntaxes of either language and wondering if someone could help me out with this.

    Base 2
    Fcnncu"Nqxgu"Lguug##

  2. #2
    Frenzied Member
    Join Date
    Apr 2009
    Location
    CA, USA
    Posts
    1,516

    Re: get count of entries between dates

    I'd use MySQL's date functions without involving PHP:
    Code:
    SELECT COUNT(closed) FROM closedtickets WHERE closed>DATE_SUB(closed,INTERVAL 10 DAY)

  3. #3
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: get count of entries between dates

    because he's using UNIX timestamps, you'll need to use FROM_UNIXTIME() to convert closed to a MySQL date format. also, you should be checking against the current date, because closed will never be above closed minus 10 days.

    Code:
    SELECT * FROM table WHERE FROM_UNIXTIME(closed) > DATE_SUB(NOW(), INTERVAL 10 DAY)
    Last edited by kows; Jul 6th, 2010 at 03:59 PM.

  4. #4
    Frenzied Member
    Join Date
    Apr 2009
    Location
    CA, USA
    Posts
    1,516

    Re: get count of entries between dates

    closed will never be above closed minus 10 days
    Where's the button to subtract from my rep?

  5. #5

    Thread Starter
    Hyperactive Member half flung pie's Avatar
    Join Date
    Jun 2005
    Location
    South Carolina, USA
    Posts
    317

    Re: get count of entries between dates

    Awesome, kows!

    My final code:
    Code:
    	$q = 'SELECT * FROM closedtickets WHERE FROM_UNIXTIME(closed) > DATE_SUB(NOW(), INTERVAL 15 DAY)';
    	$r = mysql_query($q);
    	$num_rows = mysql_num_rows($r);
    	echo "$num_rows tickets closed in the past 15 days.";
    SQL surprises me every day

    Base 2
    Fcnncu"Nqxgu"Lguug##

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width