Jump to content

PHP calendar help


ppunk

Recommended Posts

Hi, I have been playing around with getting a calendar on my upcoming site, and finally got it working last night. I found it at: [a href=\"http://keithdevens.com/software/php_calendar\" target=\"_blank\"]http://keithdevens.com/software/php_calendar[/a]
This calendar allowed me to modify it so I can make certain days links to other pages. I didn't want links, just certain dates highlighted in another color. I did this by using an array and applying CSS styles to each one. I connected to my MySQL database and pulled dates from the selected id number. In my database, I have it setup like this: date1, date2, date3, date4, date5. I am only allowing the customer to enter up to five days, so there are 5 text fields for them to fill out.
This is how it would be entered if they were having an event for the first week of April (M-F): Example: 3, 4, 5, 6, 7.
I have it working so whatever dates are in the database for that id number, it grabs them and puts them in the array to display the highlighted dates on the calendar.
Now here's my problem, if someone enters in dates at the end of the month, such as March 31 and April 1, it would get entered in the database as: 31, 1.
On the calendar, it would highlight the 1st and the 31st of March. It should only highlight the 31st and then on the April calendar it should highlight the 1st.
I have no idea how to get this to work. My guess is I have to come up with a new system for entering the dates in the database. I have been thinking of how I can fix this but can't come up with anything.
I am still in the testing phase so I can add/edit/delete any of the database field names (date1, date2, etc).

Here is the code for displaying the calendar (calendar2.php)
[code]
<?php
echo "<link href=\"../css/default.css\" rel=\"stylesheet\" type=\"text/css\" />";

mysql_pconnect("localhost","myusername","mypassword");
mysql_select_db("mydatabase");

$result = mysql_query("SELECT * FROM listings WHERE ysid=6");

while ($row = mysql_fetch_array($result)) {
    $date1 = $row["date1"];
    $date2 = $row["date2"];
    $date3 = $row["date3"];
    $date4 = $row["date4"];
    $date5 = $row["date5"];
    $ysid = $row["ysid"];
}
include 'calendar_code2.php';

    $time = time();
    $today = date('j',$time);

    $days = array(
    $date1=>array(NULL,NULL,'<div class="calendar-HL">'.$date1.'</div>'),
    $date2=>array(NULL,NULL,'<div class="calendar-HL">'.$date2.'</div>'),
    $date3=>array(NULL,NULL,'<div class="calendar-HL">'.$date3.'</div>'),
    $date4=>array(NULL,NULL,'<div class="calendar-HL">'.$date4.'</div>'),
    $date5=>array(NULL,NULL,'<div class="calendar-HL">'.$date5.'</div>'),
    $today=>array(NULL,NULL,'<div class="calendarToday">'.$today.'</div>')
    );

    echo generate_calendar(date('Y', $time), date('n', $time), $days);

?>
[/code]

And here is the code that makes the calendar work (calendar_code2.php):
[code]
<?php
# PHP Calendar (version 2.3), written by Keith Devens
# http://keithdevens.com/software/php_calendar
#  see example at http://keithdevens.com/weblog
# License: http://keithdevens.com/software/license

function generate_calendar($year, $month, $days = array(), $day_name_length = 1, $month_href = NULL, $first_day = 0, $pn = array()){
    $first_of_month = gmmktime(0,0,0,$month,1,$year);
    #remember that mktime will automatically correct if invalid dates are entered
    # for instance, mktime(0,0,0,12,32,1997) will be the date for Jan 1, 1998
    # this provides a built in "rounding" feature to generate_calendar()

    $day_names = array(); #generate all the day names according to the current locale
    for($n=0,$t=(3+$first_day)*86400; $n<7; $n++,$t+=86400) #January 4, 1970 was a Sunday
        $day_names[$n] = ucfirst(gmstrftime('%A',$t)); #%A means full textual day name

    list($month, $year, $month_name, $weekday) = explode(',',gmstrftime('%m,%Y,%B,%w',$first_of_month));
    $weekday = ($weekday + 7 - $first_day) % 7; #adjust for $first_day
    $title   = htmlentities(ucfirst($month_name)).' '.$year;  #note that some locales don't capitalize month and day names

    #Begin calendar. Uses a real <caption>. See http://diveintomark.org/archives/2002/07/03
    @list($p, $pl) = each($pn); @list($n, $nl) = each($pn); #previous and next links, if applicable
    if($p) $p = '<span class="calendar-prev">'.($pl ? '<a href="'.htmlspecialchars($pl).'">'.$p.'</a>' : $p).'</span> ';
    if($n) $n = ' <span class="calendar-next">'.($nl ? '<a href="'.htmlspecialchars($nl).'">'.$n.'</a>' : $n).'</span>';
    $calendar = '<table class="calendar" cellspacing=\"0\" cellpadding=\"3\">'."\n".
        '<caption class="calendarHeader">'.$p.($month_href ? '<a href="'.htmlspecialchars($month_href).'">'.$title.'</a>' : $title).$n."</caption>\n<tr class=\"calendarHeader2\">";

    if($day_name_length){ #if the day names should be shown ($day_name_length > 0)
        #if day_name_length is >3, the full name of the day will be printed
        foreach($day_names as $d)
            $calendar .= '<th abbr="'.htmlentities($d).'">'.htmlentities($day_name_length < 4 ? substr($d,0,$day_name_length) : $d).'</th>';
        $calendar .= "</tr>\n<tr>";
    }

    if($weekday > 0) $calendar .= '<td colspan="'.$weekday.'"> </td>'; #initial 'empty' days
    for($day=1,$days_in_month=gmdate('t',$first_of_month); $day<=$days_in_month; $day++,$weekday++){
        if($weekday == 7){
            $weekday   = 0; #start a new week
            $calendar .= "</tr>\n<tr>";
        }
        if(isset($days[$day]) and is_array($days[$day])){
            @list($link, $classes, $content) = $days[$day];
            if(is_null($content))  $content  = $day;
            $calendar .= '<td'.($classes ? ' class="'.htmlspecialchars($classes).'">' : '>').
                ($link ? '<a href="'.htmlspecialchars($link).'">'.$content.'</a>' : $content).'</td>';
        }
        else $calendar .= "<td>$day</td>";
    }
    if($weekday != 7) $calendar .= '<td colspan="'.(7-$weekday).'"> </td>'; #remaining "empty" days

    return $calendar."</tr>\n</table>\n";
}
?>
[/code]

Here is a working example of the calendar that shows the mentioned problem:
[a href=\"http://honolulu.micfo.com/~lobster/cal/local_listings/calendar2.php\" target=\"_blank\"]http://honolulu.micfo.com/~lobster/cal/loc...s/calendar2.php[/a]


Any help would be much appreciated!
Thanks!

-Steve
Link to comment
Share on other sites

Hi, thanks for the replies.
In the $days array, $date1 = 31 and $date2 = 1 in my example.
Whatever number is in any of the date fields (date1-date5) will get put in the correct spot in the array. That is how the dates are highlighted in green squares on my calendar.
[code]
$days = array(
    $date1=>array(NULL,NULL,'<div class="calendar-HL">'.$date1.'</div>'),
    $date2=>array(NULL,NULL,'<div class="calendar-HL">'.$date2.'</div>'),
    $date3=>array(NULL,NULL,'<div class="calendar-HL">'.$date3.'</div>'),
    $date4=>array(NULL,NULL,'<div class="calendar-HL">'.$date4.'</div>'),
    $date5=>array(NULL,NULL,'<div class="calendar-HL">'.$date5.'</div>'),
    $today=>array(NULL,NULL,'<div class="calendarToday">'.$today.'</div>')
    );
[/code]
With date1 and date2 pulled looks like this:
[code]
$days = array(
    31=>array(NULL,NULL,'<div class="calendar-HL">'.31.'</div>'),
    1=>array(NULL,NULL,'<div class="calendar-HL">'.1.'</div>'),
    $date3=>array(NULL,NULL,'<div class="calendar-HL">'.$date3.'</div>'),
    $date4=>array(NULL,NULL,'<div class="calendar-HL">'.$date4.'</div>'),
    $date5=>array(NULL,NULL,'<div class="calendar-HL">'.$date5.'</div>'),
    $today=>array(NULL,NULL,'<div class="calendarToday">'.$today.'</div>')
    );
[/code]
I hope that helps describe it better.

In regards to Barand's post, do you know how I might set that up so it will pull the current year and month associated with the dates? Right now, my database table contains these:

name, email, title, month, date1, date2, date3, date4, date5, time1, time2, summary

Even if I didn't use the calendar I would run into problems beacuse if I displayed the full date for the event on the page it would look like this: March 31, 1
It should be like this: March 31, April 1
So my database has to be adjusted somehow because one month field is not going to work.

Right now I actually don't have a 'year' field, which might be a smart idea.
If I change the database fields somehow to contain the year and month for each date entered I still need it to output the day number for each day the visitor enters as 1-31 to work with that calendar array.

The calendar is going to be used for a local event site, where people can post stuff like yard sales, meetings and any other kind of event.

Can you think of a setup that would work? I have been thinking about your response and I can't come up with anything (yet).

Thanks again for the quick replies!

-Steve
Link to comment
Share on other sites

You've painted yourself into a corner with the table design. Not only does it limit to 5 dates but wastes space if they only have 1.

A better, more flexible design is

user table
[code]name, email,  time1, time2, summary[/code]

events table (row for each appointment for each user)
[code]name, title, eventdate[/code]

Now you can hold as few or as many per user and, if the eventdate is type DATE, it holds the day, month and year info.
Link to comment
Share on other sites

I thought over your last post and don't think it will work for my site. While I was thinking about it I came up with this; adding a month field for each date: month1, date1, month2, date2, month3, date3, month4, date4, month5, date5
Yes, it takes up more fields in the database, but it works for me. I want to limit the amount of dates to only 5 days for one listing. I am expecting to have alot of yard sales on my site, so in most cases there will only be 2 or 3 days entered (usually Sat. and Sun.), but I'm allowing 5 days incase anyone does M-F sales.
To get this to work with the calendar, I added this before the $days array:
[code]
$current_month = date('F');

if ($month1 == $current_month){
    $day01 = $date1;
    }
else
    echo "";
if ($month2 == $current_month){
    $day02 = $date2;
    }
else
    echo "";

if ($month3 == $current_month){
    $day03 = $date3;
    }
else
    echo "";
    
if ($month4 == $current_month){
    $day04 = $date4;
    }
else
    echo "";

if ($month5 == $current_month){
    $day05 = $date5;
    }
else
    echo "";
[/code]
Then I changed the array to:
[code]
$days = array(
    $day01=>array(NULL,NULL,'<div class="calendar-HL">'.$day01.'</div>'),
    $day02=>array(NULL,NULL,'<div class="calendar-HL">'.$day02.'</div>'),
    $day03=>array(NULL,NULL,'<div class="calendar-HL">'.$day03.'</div>'),
    $day04=>array(NULL,NULL,'<div class="calendar-HL">'.$day04.'</div>'),
    $day05=>array(NULL,NULL,'<div class="calendar-HL">'.$day05.'</div>'),
    $today=>array(NULL,NULL,'<div class="calendarToday">'.$today.'</div>')
    );
[/code]
There might be a more advanced way to simplify this problem, but this is working great for me now. I might be changing it from 5 days to 3 days, so the database won't be as messy and the php cleaner.
I am going to maintain the database so entries get removed often (once they expire from their entered dates) so the database doesn't get too filled up and so they don't display on the main page, because I don't want the expired events mixed in with the new/upcoming ones.

I entered a couple fake entries into my database with different months/days and it filtered all accordingly.
I am going to take your advice on dividing the database into two tables. I am going to create a user table with the persons name and email (might turn it into a members table instead with all contact info).

I think that concludes this problem. Thanks again for the posts!

-Steve
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.