Listing dates as Sept 2-4,7-9 & Oct 4,5,6 etc.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
lamamike
Forum Newbie
Posts: 1
Joined: Tue Mar 30, 2004 12:27 pm

Listing dates as Sept 2-4,7-9 & Oct 4,5,6 etc.

Post by lamamike »

I'm a newbie to dates in php and MySQL, and I have spent hours looking through forums for an answer to what I'm trying to do... I've found plenty on displaying single dates and comparing two dates, for example, but nothing on listing many dates...

Sorry if it's overkill, but I'll explain what I'm trying to do in detail so the real coders can point me in the right direction properly...

I've built a form to input lots of dates into a single row of a MySQL table. Specifically, I've got two kinds of dates: multi-day events (where I have a startdate and enddate) and single-day events. Each entry (row) in the table has up to 10 such start- and end-dates and 30 single-day event dates. The dates are of the 'date' type, such that they are MM-DD-YYYY in the db.

As an example, consider I want to list three multi-day events and two single days after each of multi-day events. I don't want to have the month displayed again and again, just once for each set of dates...

I'm trying to display the multi-day events as: Sept 22-28 / Oct. 2-8 and 14-18
for example


And display the single-day events as: Sept. 29 / Oct. 1,9,10,19 and 20
for example


I'm looking for some dates functions to help me sift through the raw MySQL date terms to make em pretty! Any help you can give would be very appreciated!
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

1- First, I'd strongly consider changing your date format from MySQL's date type to a UNIX timestamp. If there are any advantages to using MySQL's date format, I haven't found any. UNIX timestamps make it much easier to retrieve information about the date such as day of month, month, day of week, and pretty much anything else you could want.

2 - Date format aside, I would suggest getting the full dates of all events first. Then, group all events in the same month together. Finally, display the common month followed by the days.

For example:

If you've got multiple single day events in October, you'd have entries
10-01-2004, 10-09-2004, 10-10-2004, etc. You could retrieve all dates in month 10, output Oct, then all the days.

Does this make sense?
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
kettle_drum
DevNet Resident
Posts: 1150
Joined: Sun Jul 20, 2003 9:25 pm
Location: West Yorkshire, England

Post by kettle_drum »

Do as pickle suggests and store the value as the unix time stamp, and then also store the length of the event in seconds - so like 86400 for a single day and 259200 for 3 days.

Then you can simplycall these values and use date() to display them all pretty in an english format, and then keep adding 86400 to the start date until the length value is 0, and you will know how many days has passed, and you can once again use date() to get the values, even if an event passes from one month to another.
Post Reply