Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

How do I correctly sort by date using a php/mysql query?

Posted on 2007-11-29
5
708 Views
Last Modified: 2013-12-12
OK, I have a form that allows users to seach a database of events. Each event is sepearated by categories and they have a start date and end date. They can only search by category and/or by month, so the possible searches would be:

All categories and no months
1 category and no month
1 category with month
no category and just month

So basically If the following was the events by begin date
12/1/2007 - Event 1
12/6/2007 - Event 2
1/6/2008 - Event 3
1/16/2008 - Event 4
12/2/2007 Event 5
2/20/2008 - Event 6
4/16/2008 - Event 7
What I want it to do is when I search by month, I want to start with the month I search by and output in order. So if the search was for January and january has already happened this year it would default to the next year so the output needs to be

1/6/2008 - Event 3
1/16/2008 - Event 4
2/20/2008 - Event 6
4/16/2008 - Event 7

And if the search month was december I need the output to be
12/1/2007 - Event 1
12/6/2007 - Event 2
1/6/2008 - Event 3
1/16/2008 - Event 4
2/20/2008 - Event 6
4/16/2008 - Event 7

So I really need the month to start on the selected month from the search form and then output every event after that in order.

Anyone like to give this a shot?
Thanks
0
Comment
Question by:tech1984
5 Comments
 
LVL 21

Expert Comment

by:nizsmo
ID: 20377831
Can you give more detail in terms of what type of date field that is? also a short code snippet of your current working code so that maybe we can modify accordingly?
0
 
LVL 50

Expert Comment

by:Steve Bink
ID: 20386287
I've found the easiest way to deal with dates in MySQL is to use the unix timestamp format.  That provides for a strict ordering from earliest date to latest date.  You can see examples of its usage at:

http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_unix-timestamp
0
 
LVL 2

Accepted Solution

by:
SerbanGhita earned 500 total points
ID: 20388164
check the code below, i also made a demo table so i can play with. the solution as you want it is simple.

note: i would put a filter on month + year. i mean if i select a month its obvious that i know what year i want the event to be.
<?php
/*
 
CREATE TABLE `events` (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`date` DATE NOT NULL ,
`event` VARCHAR( 100 ) NOT NULL
) ENGINE = innodb;
 
INSERT INTO `events` 
(`date` , `event`)
VALUES 
('2007-12-06', 'Event 1'), 
('2007-12-01', 'Event 2'),
('2007-04-16', 'Event 3'),
('2007-01-16', 'Event 4'),
('2007-01-12', 'Event 5'),
('2007-02-20', 'Event 6'),
('2007-01-06', 'Event 7'),
('2007-04-21', 'Event 8'),
('2008-01-21', 'Event 9'),
('2008-01-18', 'Event 10'),
('2008-12-21', 'Event 11')
;
 
 
mysql> SELECT * FROM `events` WHERE `date` >= '2008-01-01' ORDER BY `date` ASC
    -> ;
+----+------------+----------+
| id | date       | event    |
+----+------------+----------+
| 10 | 2008-01-18 | Event 10 |
|  9 | 2008-01-21 | Event 9  |
| 11 | 2008-12-21 | Event 11 |
+----+------------+----------+
3 rows in set (0.00 sec)
 
mysql> SELECT * FROM `events` WHERE `date` >= '2007-12-01' ORDER BY `date` ASC
    -> ;
+----+------------+----------+
| id | date       | event    |
+----+------------+----------+
|  2 | 2007-12-01 | Event 2  |
|  1 | 2007-12-06 | Event 1  |
| 10 | 2008-01-18 | Event 10 |
|  9 | 2008-01-21 | Event 9  |
| 11 | 2008-12-21 | Event 11 |
+----+------------+----------+
5 rows in set (0.00 sec)
 
 
 
*/
 
$POST_month = '12'; //12
 
if(date('m')>$POST_month){
	$POST_year = date('Y')+1;
} else {
	$POST_year = date('Y');
}
$query = "
	SELECT * 
	FROM `events` 
	WHERE
	`date` >= '".$POST_year."-".$POST_month."-01'
	";
 
$query .= " ORDER BY `date` ASC ";
 
print $query;
 
?>

Open in new window

0
 

Author Closing Comment

by:tech1984
ID: 31411799
Thank you! perfect
0
 

Author Comment

by:tech1984
ID: 20395419
Perfect! Thanks
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
What is wrong with this PDO Delete Query? 2 17
Extracting store locations from Google maps or site 2 22
Link failure 16 32
Log in through ID 5 17
Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
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…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

839 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