Jump to content

Archived

This topic is now archived and is closed to further replies.

DaveLinger

PHP Calendar Help

Recommended Posts

So I'm working on making a PHP Calendar for my site, in which it will check a sql row for each day to see if there are any events, and either echo them or say nothing (I can do that part).

Heres the part where it gets tricky.

I have built a set of tables, 7 wide by 5 high, to be used when my calendar is drawn. The things php needs to know before it draws it is:

* What day of the week to put day 1 on

* How many days are in the month

I have an idea of how to get both, but nothing solid. We can achieve getting the day of the week to start on by setting each day of the week to have an id number, like this:

1 = Sunday
2 = Monday
3 = Tuesday
4 = Wednesday
5 = Thursday
6 = Friday
7 = Saturday

Then we just find which id the LAST day of the past week used, and add 1 to it to get the starting day for the first.

Here's what I have in mind:
[code]<table border="1">
    <tr>
        <td height="20" width="150">Sunday</td>
        <td height="20" width="150">Monday</td>
        <td height="20" width="150">Tuesday</td>
        <td height="20" width="150">Wednesday</td>
        <td height="20" width="150">Thursday</td>
        <td height="20" width="150">Friday</td>
        <td height="20" width="150">Saturday</td>
    </tr>
    <tr>
        <td height="125" width="150"><? echo '$day1' ?></td>
        <td height="125" width="150"><? echo '$day2' ?></td>
        <td height="125" width="150"><? echo '$day3' ?></td>
        <td height="125" width="150"><? echo '$day4' ?></td>
        <td height="125" width="150"><? echo '$day5' ?></td>
        <td height="125" width="150"><? echo '$day6' ?></td>
        <td height="125" width="150"><? echo '$day7' ?></td>
    </tr>
[/code]

(I'd do those rows 4 more times for the other days)

Then we just have to, based on the number of days in the month, and the days on which the last month ended, set, for instance,

$day1 = ""
$day2 = "1st"
$day3 = "2nd"

etc. til the end of the month. The number of the days in the month can easily be determined by setting variables like this:

number1 = 31 (January)
number2 = 28 (February)
number3 = 31
number4 = 30

and get it so that after it sets 31 (or 28, or 30) variables as "1st, 2nd, 3rd", it will set the rest as "".

Am I completely crazy or is there an easier way to do this?

Share this post


Link to post
Share on other sites
I've got a calendar script that i picked up online somewhere, it is a really basic calendar, but its got loads of complicated code in it, and the maths really messed with my head for a while.

I would say, without comparing the code, that you are probably heading along the right lines.

Share this post


Link to post
Share on other sites
dave, you've got a great idea, but there is a simpler way to do it. first of all, the date() function has built in a way to check what day of the week any given day comes on. so, once you've got that, you can simply add the count of the number of days in the current month to the end of that. then, if there are any extra empty spots, fill them in as well. one reason you need to do it dynamically this way is that not all months will fit on 4 or even 5 weeks. you've got months every once in a while that require 6 rows! so, with that in mind, check this idea out and see if it makes sense. this is how i use it in my calendar app i'm writing:

[code]
<?php
// set first day of current month
$month = date('n');
$year = date('Y');
$date = mktime(0,0,0,$month, 1, $year);

// what day of the week does the first day appear on?
// also nicely serves to tell us how many empty days we will have
$offset = date('w', $date);

// how many days until the last day of the month?
$totalDays = $offset + date('t', $date);

echo "<table>\n"; // start table

// set a day variable starting with the blank days
$i = NULL;
for ($i = 1; $i <= $totalDays; ++$i) {
    $day = $i - $offset;
    if ($i % 7 == 1) echo "<tr>\n"; // starting a new week
    if ($day < 1) echo "<td class=\"nonMonthDay\"></td>\n"; // non month day
    else {
        // it's a month day, run any checks you want here for content
        $thisDate = date('Y-m-d', mktime(0,0,0,$month,$day,$year));
        echo "<td class=\"monthDay\">$day</td>\n";
    }
    if ($i % 7 == 0) echo "</tr>\n"; // ending the week
}

// check and see if we ended the week in full
$x = 7 - (($i - 1) % 7);
if ($x > 0) {
    for ($i = 0; $i < $x; ++$i) echo "<td class=\"nonMonthDay\"></td>\n";
    echo "</tr>\n";
}

echo "</table>\n"; // end table
?>
[/code]

hope this helps

Share this post


Link to post
Share on other sites
.. if all else fails, you can always download mine and use it.

[a href=\"http://www.digitalmidget.com/php_noob/calendar.php\" target=\"_blank\"]http://www.digitalmidget.com/php_noob/calendar.php[/a]

Share this post


Link to post
Share on other sites
[!--quoteo(post=386434:date=Jun 21 2006, 11:11 AM:name=obsidian)--][div class=\'quotetop\']QUOTE(obsidian @ Jun 21 2006, 11:11 AM) [snapback]386434[/snapback][/div][div class=\'quotemain\'][!--quotec--]
dave, you've got a great idea, but there is a simpler way to do it. first of all, the date() function has built in a way to check what day of the week any given day comes on. so, once you've got that, you can simply add the count of the number of days in the current month to the end of that. then, if there are any extra empty spots, fill them in as well. one reason you need to do it dynamically this way is that not all months will fit on 4 or even 5 weeks. you've got months every once in a while that require 6 rows! so, with that in mind, check this idea out and see if it makes sense. this is how i use it in my calendar app i'm writing:

[code]
<?php
// set first day of current month
$month = date('n');
$year = date('Y');
$date = mktime(0,0,0,$month, 1, $year);

// what day of the week does the first day appear on?
// also nicely serves to tell us how many empty days we will have
$offset = date('w', $date);

// how many days until the last day of the month?
$totalDays = $offset + date('t', $date);

echo "<table>\n"; // start table

// set a day variable starting with the blank days
$i = NULL;
for ($i = 1; $i <= $totalDays; ++$i) {
    $day = $i - $offset;
    if ($i % 7 == 1) echo "<tr>\n"; // starting a new week
    if ($day < 1) echo "<td class=\"nonMonthDay\"></td>\n"; // non month day
    else {
        // it's a month day, run any checks you want here for content
        $thisDate = date('Y-m-d', mktime(0,0,0,$month,$day,$year));
        echo "<td class=\"monthDay\">$day</td>\n";
    }
    if ($i % 7 == 0) echo "</tr>\n"; // ending the week
}

// check and see if we ended the week in full
$x = 7 - (($i - 1) % 7);
if ($x > 0) {
    for ($i = 0; $i < $x; ++$i) echo "<td class=\"nonMonthDay\"></td>\n";
    echo "</tr>\n";
}

echo "</table>\n"; // end table
?>
[/code]

hope this helps
[/quote]


ok that works, I went ahead and tweaked it to fit my needs, I just have one small problem, I want TODAY to be red, instead of black. Here's what I've got:

[code]$today = date("d");

if ($day == $today);

{

echo "<font color=\"red\">$day</font>";

}[/code]

How would I work that in?

Share this post


Link to post
Share on other sites
[!--quoteo(post=386451:date=Jun 21 2006, 11:37 AM:name=DaveLinger)--][div class=\'quotetop\']QUOTE(DaveLinger @ Jun 21 2006, 11:37 AM) [snapback]386451[/snapback][/div][div class=\'quotemain\'][!--quotec--]
...I want TODAY to be red, instead of black.
How would I work that in?
[/quote]

well, take a look at this section of code:
[code]
<?php
else {
    // it's a month day, run any checks you want here for content
    $thisDate = date('Y-m-d', mktime(0,0,0,$month,$day,$year));
    echo "<td class=\"monthDay\">$day</td>\n";
}
?>
[/code]

you already have $thisDate with the date of the current day, so just do your comparison there:

obviously, i'm assuming that $today holds today's date in YYYY-MM-DD format
[code]
<?php
else {
    // it's a month day, run any checks you want here for content
    $thisDate = date('Y-m-d', mktime(0,0,0,$month,$day,$year));
    if ($today == $thisDate) echo <td class=\"monthDay\" style=\"color: #ff0000\">$day</td>\n";
    else echo "<td class=\"monthDay\">$day</td>\n";
}
?>
[/code]

that should fix it for you.

Share this post


Link to post
Share on other sites
[!--quoteo(post=386468:date=Jun 21 2006, 12:26 PM:name=obsidian)--][div class=\'quotetop\']QUOTE(obsidian @ Jun 21 2006, 12:26 PM) [snapback]386468[/snapback][/div][div class=\'quotemain\'][!--quotec--]obviously, i'm assuming that $today holds today's date in YYYY-MM-DD format[/quote]
[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]what should I have for the value of $today?[/quote]
[code]$today = date("Y-m-d"); // today in yyyy-mm-dd format[/code]

Share this post


Link to post
Share on other sites
Thanks. I'm not very familiar with PHP's date format, I've really only used MySQL's

Share this post


Link to post
Share on other sites

×

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.