How to create a calendar with MYSQL data?

There are some days with data in my database and some without; I have one column with data and another column with date and time. So I want to create this calendar. So it will show all days of the month and then the days with data will be hyperlinked.

When I click on the link, it will show all the data submitted for that day. I would probably use a loop in the first loop. One for a month and then one for display every day.

However, since each month of the year has a different number of days, and also the leap year is a problem, I don't know how to write the conditions.

Any help or guidance is appreciated.

+2


source to share


2 answers


$start = '2009-01-01';
$current = strtotime($start);
while(date('n',$current)==date('n',strtotime($start))) {
    // do your stuff here, $current includes the current date.
    // the loop will run through the complete month

    $current = strtotime(date('Y-m-d',$current) . '+1 day');
}

      



+1


source


You need to know first what day of the week the month starts, so you know how many empty boxes to spit out. Then figure out how many days there are in that month. Then go through your days, wrapping up to the next line after Saturday. Then fill in the rest of the last line with a blank field.

There's some pretty simple (and commented out) code that does it here:

http://gitorious.org/wfpl/wfpl/blobs/master/calendar.php



You can use this code without the rest of the framework it is for, if you just rewrite calender_day()

(which is called for every cell in the calendar, with the first parameter telling you what day it is (not in a month, has events, no events) and rewrite calendar_week()

which is called at the end of each line.

Or you could just look at how to do the relevant things, like finding out how far in a week a month starts, etc.

0


source







All Articles