Jump to content


Photo

PHP Calendar Help


  • Please log in to reply
8 replies to this topic

#1 DaveLinger

DaveLinger
  • Members
  • PipPipPip
  • Advanced Member
  • 268 posts
  • LocationWV, USA

Posted 21 June 2006 - 02:23 PM

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:
<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>

(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?
http://www.DaveLinger.com
dave at linger dot com

#2 wisewood

wisewood
  • Members
  • PipPipPip
  • Advanced Member
  • 226 posts
  • LocationRotherham, England

Posted 21 June 2006 - 03:02 PM

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.
wisewood: proven fact, I am both wise, and wooden.

#3 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 21 June 2006 - 03:11 PM

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:

<?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
?>

hope this helps
You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

#4 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 21 June 2006 - 03:18 PM

.. 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]
Legend has it that reading the manual never killed anyone.
My site

#5 DaveLinger

DaveLinger
  • Members
  • PipPipPip
  • Advanced Member
  • 268 posts
  • LocationWV, USA

Posted 21 June 2006 - 03:37 PM

[!--quoteo(post=386434:date=Jun 21 2006, 11:11 AM:name=obsidian)--][div class=\'quotetop\']QUOTE(obsidian @ Jun 21 2006, 11:11 AM) View Post[/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:

<?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
?>

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:

$today = date("d");

if ($day == $today);

{

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

}

How would I work that in?
http://www.DaveLinger.com
dave at linger dot com

#6 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 21 June 2006 - 04:26 PM

[!--quoteo(post=386451:date=Jun 21 2006, 11:37 AM:name=DaveLinger)--][div class=\'quotetop\']QUOTE(DaveLinger @ Jun 21 2006, 11:37 AM) View Post[/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:
<?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";
}
?>

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
<?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";
}
?>

that should fix it for you.
You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

#7 DaveLinger

DaveLinger
  • Members
  • PipPipPip
  • Advanced Member
  • 268 posts
  • LocationWV, USA

Posted 21 June 2006 - 05:08 PM

what should I have for the value of $today?
http://www.DaveLinger.com
dave at linger dot com

#8 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 21 June 2006 - 05:11 PM

[!--quoteo(post=386468:date=Jun 21 2006, 12:26 PM:name=obsidian)--][div class=\'quotetop\']QUOTE(obsidian @ Jun 21 2006, 12:26 PM) View Post[/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]
$today = date("Y-m-d"); // today in yyyy-mm-dd format

Legend has it that reading the manual never killed anyone.
My site

#9 DaveLinger

DaveLinger
  • Members
  • PipPipPip
  • Advanced Member
  • 268 posts
  • LocationWV, USA

Posted 21 June 2006 - 05:16 PM

Thanks. I'm not very familiar with PHP's date format, I've really only used MySQL's
http://www.DaveLinger.com
dave at linger dot com




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users