Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9167233
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T15:20:23+00:00 2026-06-17T15:20:23+00:00

How can I sort in PHP a SQL query grouping by date… I have

  • 0

How can I sort in PHP a SQL query grouping by date…

I have this table

id  date                  Title
1   2012-12-19 11:00:00   Value 10
2   2013-01-04 09:00:00   Value 15
3   2013-01-04 20:00:00   Value 18
4   2013-01-04 21:00:00   Value 5
5   2013-01-07 09:00:00   Value 8
6   2013-01-07 09:40:00   Value 11
7   2013-01-07 11:00:00   Value 12

And I would like to sort by month like so

December 19
11:00:00      Value 10

January 04
09:00:00      Value 15
20:00:00      Value 18
21:00:00      Value 5

January 07
09:00:00      Value 8
09:40:00      Value 11
11:00:00      Value 12
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T15:20:25+00:00Added an answer on June 17, 2026 at 3:20 pm

    Try below query and use PHP loop to get your desired result. You just need to split id, time and title values inside your PHP loop to format data for display.

    SELECT
    GROUP_CONCAT(id) AS id,
    DATE_FORMAT(date,'%M %e') AS Date,
    GROUP_CONCAT(DATE_FORMAT(date,'%H:%i:%s')) AS TimeValue,
    GROUP_CONCAT(title) AS Title
    FROM abc
    GROUP BY DATE(date)
    ORDER BY MONTH(date) DESC
    

    OUTOUT

    ID          DATE            TIMEVALUE                       TITLE
    1           December 19     11:00:00                        Value 10
    2,3,4       January 4       09:00:00,20:00:00,21:00:00      Value 15,Value 18,Value 5
    5,6,7       January 7       09:00:00,09:40:00,11:00:00      Value 8,Value 11,Value 12
    

    SQL fiddle: http://sqlfiddle.com/#!2/ec3a8/6

    UPDATE:

    OUTPUT

    ID          DATE                TIMEVALUE
    1           December 19         11:00:00___Value 10
    2,3,4       January 4           09:00:00___Value 15,20:00:00___Value 18,21:00:00___Value 5
    5,6,7       January 7           09:00:00___Value 8,09:40:00___Value 11,11:00:00___Value 12
    

    PHP CODE: I’m assuming you have array like below on the basis of above query output.

    <?php
    $data = array(
            0=>array(
                'id'=>1,
                'date'=>'December 19',
                'time_value'=>'11:00:00___Value 10',
            ),
            1=>array(
                'id'=>2,3,4,
                'date'=>'January 4',
                'time_value'=>'09:00:00___Value 15,20:00:00___Value 18,21:00:00___Value 5',
            ),
            2=>array(
                'id'=>5,6,7,
                'date'=>'January 7',
                'time_value'=>'09:00:00___Value 8,09:40:00___Value 11,11:00:00___Value 12',
            )    
        );
    if(is_array($data))
    {
        foreach($data as $value)
        {
            if(is_array($value))
            {
                echo '<br />'.$value['date'].'<br />';
                $arrTime = explode(',', $value['time_title']);
                if($arrTime)
                {
                    foreach($arrTime as $time)
                    {
                        $arrTimeTitle = explode('_', $time);
                        echo $arrTimeTitle[0]."&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;".$arrTimeTitle[1]."<br />";
                    }
                }
            }
        }
    }
    ?>
    

    Final PHP output:

    December 19
    11:00:00     Value 10
    
    January 4
    09:00:00     Value 15
    20:00:00     Value 18
    21:00:00     Value 5
    
    January 7
    09:00:00     Value 8
    09:40:00     Value 11
    11:00:00     Value 12
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a query something like this: SELECT title, desc, date FROM tablename ORDER
I have this php script that generate an sql query. However, it needs a
I have this query which I want to run in my PHP application back
im very new to this and have a very basic knowledge php and SQL
I have a table/array. E.g. in Excel I can sort by column 1 asc,
How can I sort a 2D array in PHP. I want to sort on
I can sort by key or value, but I need it sorted by value,
The UNIX sort command can sort a very large file like this: sort large_file
I have been trying to create a ListView which I can sort using drag
Can SQLite sort naturally? For example, CREATE TABLE animals ( id INTEGER NOT NULL

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.