Outputting MySQL Statment For Grouping Dates

Discussion in 'PHP' started by jonathandey, Oct 8, 2010.

  1. #1
    I have a problem outputting the following mysql statement.

    What I'd like to do with it is show how many values have the same date.
    
    07/10/2010 - 2
    06/10/2010 - 10
    
    Code (markup):
    My view_date entries in the database look like this
    1286454350, 1286454350, 1286454350, 1286363232, 1286363232, 1286363232, 1286363232, 1286479111
    Code (markup):

    
    $q=mysql_query("SELECT
        DATE_FORMAT(FROM_UNIXTIME(view_date), '%m-%d-%Y') AS sdate,
        COUNT(id) AS scount
    FROM
        $tbl
    WHERE
        view_date <= UNIX_TIMESTAMP(NOW())
        AND view_date UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL 30 DAY))
    GROUP BY
        sdate");
    
    PHP:
    Thanks In Advanced.
     
    jonathandey, Oct 8, 2010 IP
  2. s_ruben

    s_ruben Active Member

    Messages:
    735
    Likes Received:
    26
    Best Answers:
    1
    Trophy Points:
    78
    #2
    You can get the values from database ( $q = mysql_query("SELECT `view_date` FROM $tbl"); ) and count the view_date by PHP script. I think this script example will help you to do it:

    
    function convert_dates(&$value){
        $value = date("d/m/Y",$value);
    }
    
    $dates = array(1286454350, 1286454350, 1286454350, 1286363232, 1286363232, 1286363232, 1286363232, 1286479111);
    
    array_walk($dates,"convert_dates");
    
    print_r(array_count_values($dates));
    
    PHP:
     
    s_ruben, Oct 8, 2010 IP
  3. jonathandey

    jonathandey Active Member

    Messages:
    112
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    53
    #3
    Worked like clock work, thanks again.
     
    jonathandey, Oct 8, 2010 IP