?
Solved

PHP - Group by month?

Posted on 2009-02-19
4
Medium Priority
?
288 Views
Last Modified: 2013-12-12
Okay, so when I pull data from my table, I'll get (minimally):

start | end | description
------------------------------------------------------------------------------------------------
2009-03-01T09:00:00 | 2009-03-01T11:00:00 | Lorem Ipsum
2009-03-11T09:00:00 | 2009-03-11T12:00:00 | Donec ullamcorper
2009-03-21T09:00:00 | 2009-03-23T09:00:00 | Morbi consectetur
2009-04-05T09:00:00 | 2009-04-07T09:00:00 | Aliquam fringilla
2009-04-17T09:00:00 | 2009-04-17T11:00:00 | Suspendisse consectetur
2009-06-09T09:00:00 | 2009-06-11T09:00:00 | Sed ut nibh a tellus
2009-06-23T09:00:00 | 2009-06-23T12:00:00 | Quisque porta
------------------------------------------------------------------------------------------------

I'm trying to (1) group them by month (2) only show date range when it lasts more than a day (3) not show months that have nothing (i.e., May)

So, the output would look like this:

March 2009
- Lorem Ipsum, 1 March 2009
- Donec ullamcorper, 11 March 2009
- Morbi consectetu, 21 March 2009 to 23 March 2009

April 2009
- Aliquam fringilla, 5 April 2009 to 7 April 2009
- Suspendisse consectetur, 17 April 2009

June 2009
- Sed ut nibh a tellus, 9 June 2009 to 11 June 2009
- Quisque porta, 23 June 2009

I'm getting errors when trying to group by month, and the output of the date is completely wrong.  Help!
0
Comment
Question by:n00b0101
  • 3
4 Comments
 
LVL 8

Expert Comment

by:agamal
ID: 23687720
try to select from your table using
LIKE %2009-03% for March
LIKE %2009-04% for April

and so on
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 23687722
Can you please show us the code that is causing the errors?
0
 
LVL 111

Accepted Solution

by:
Ray Paseur earned 2000 total points
ID: 23687867
And I guess we need to see your DB query, too.  You might be able to SELECT MONTH, or you might be able to iterate over the entire results set.  Not sure which would be easier for you - it might depend on the number of rows in the table.
<?php // RAY_temp_datex.php
 
error_reporting(E_ALL);
 
// TEST DATA ARRAY
$td = array();
$td[] = "2009-03-01T09:00:00 | 2009-03-01T11:00:00 | Lorem Ipsum";
$td[] = "2009-03-11T09:00:00 | 2009-03-11T12:00:00 | Donec ullamcorper";
$td[] = "2009-03-21T09:00:00 | 2009-03-23T09:00:00 | Morbi consectetur";
$td[] = "2009-04-05T09:00:00 | 2009-04-07T09:00:00 | Aliquam fringilla";
$td[] = "2009-04-17T09:00:00 | 2009-04-17T11:00:00 | Suspendisse consectetur";
$td[] = "2009-06-09T09:00:00 | 2009-06-11T09:00:00 | Sed ut nibh a tellus";
$td[] = "2009-06-23T09:00:00 | 2009-06-23T12:00:00 | Quisque porta";
 
// AN IMPOSSIBLE MONTH TO TRIGGER FIRST BREAKPOINT
$old_month = '00';
 
// ITERATE OVER THE TEST DATA SET
foreach($td as $data)
{
   $line     = explode('|', $data);
   $my_info  = trim($line[2]);
   
// REFORMAT THE DATES   
   $date_a   = date('Y-m-d',strtotime($line[0]));
   $date_z   = date('Y-m-d',strtotime($line[1]));
 
// EXTRACT THE CURRENT MONTH   
   $date_mo  = substr($date_a,6,2);
      
// IF THIS IS A NEW MONTH
   if ($date_mo != $old_month)
   {
      echo "<br/><br/>" . date('F Y', strtotime($date_a));
      $old_month = $date_mo;
   }
 
// PRINT THE TEXT
   echo "<br/> - $my_info, \n";
   
// PRINT THE START DATE
   echo date('j F Y', strtotime($date_a));
 
// IF THE END IS NOT THE SAME AS THE START, PRINT THE END
   if ($date_a != $date_z)
   {
      echo " to ";
      echo date('j F Y', strtotime($date_z));
   }
}

Open in new window

0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 23692258
Thanks for the points!  It's a good question and a good opportunity to show  how strtotime() and date() work together in ways that are almost magically easy.  Best regards, ~Ray
0

Featured Post

Granular recovery for Microsoft Exchange

With Veeam Explorer for Microsoft Exchange you can choose the Exchange Servers and restore points you’re interested in, and Veeam Explorer will present the contents of those mailbox stores for browsing, searching and exporting.

Question has a verified solution.

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

Windocks is an independent port of Docker's open source to Windows.   This article introduces the use of SQL Server in containers, with integrated support of SQL Server database cloning.
Microsoft Access has a limit of 255 columns in a single table; SQL Server allows tables with over 255 columns, but reading that data is not necessarily simple.  The final solution for this task involved creating a custom text parser and then reading…
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
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 …
Suggested Courses
Course of the Month16 days, 17 hours left to enroll

864 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