Calender Help!

He has: 1,758 posts

Joined: Jul 2002

Hey guys... I'm writing a PHP script to display a calendar starting from the beginning of the current month (August 2004) to the end of the current month two years away (ie August 2006). This will update automatically based on the current month and I'm pretty close to getting the script finished.

The problem is, is that the script starts at today and ends at the day before the equivalent date exactly two years away instead of starting at the start of the month and ending at the end (you still with me? Wink ) (see here for example of output - it's very basic at the moment, but will eventually output a nicely formatted table).

I've tried everything I can think of to get it working right, but I don't fully understand all of the functions I'm using, so I've kind of hit a wall.

Have you guys got any suggestions on what I need to do to get this working?

Please help! I'm beggin! Confused

Thanks! (code below).

<?php
<div align=\"center\">
<p>



$count = 0;
$monthcount = 0;

for(
$i = 0; $i < 730; $i++) {

   
$my_time = strtotime(\"+$i DAY\");

   
$see = date('d', $my_time);
   
$thismonth = date('F Y', $my_time);

   
$count++;

    if(
$count%7 == 0) {

        echo(\" <br> \");


    }

    if(
$thismonth != $lastmonth) {

       

        </p>

        <p align=\"center\"><strong>=
$thismonth </strong></p>

        <p>

       

       
$lastmonth = $thismonth;
       
$monthcount++;
       
$count = 0;

    }

    echo (\"
$see / \");
}


</p>
</div>
?>

Andy

KarenArt's picture

She has: 354 posts

Joined: May 2001

Well I am definately no php expert but it sounds like you want to show all the days in the month and not just simply count 2 years from today.... is that right?

I have played with a few calendar scripts trying to find something to suit my needs and the simplest way I know of to find the days in a month is...

$daysIM = DayInMonth($thismonth,$year);
function DayInMonth($thismonth, $year)
{
   $daysInMonth = array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
   if ($month != 2)
   {
    return $daysInMonth[$month - 1];
    }
    else
    {
   return (checkdate($month, 29, $year)) ? 29 : 28;
    }
}
'

You will need to do a quick bit of math $year = $thisyear+1 in a loop for each year you want, but I'm pretty sure that will get you all the days displayed.

If I'm totally wrong... I hope someone will correct me. (There's always a first time for being wrong...LOL)

gotta finish redesigning my sites so I can show them again.

The purpose of education is... to get more jokes!

He has: 46 posts

Joined: Jun 2004

I haven't done much with PHP date functions so I can't really give you code here. Sorry about that.

I do see where the problem is, though. In your for loop you're only allowing two years worth of days. What you really want is two years plus however many days are in this month, for a total of 25 months. (Today, August should be in there three times: 2004, 2005, and 2006).

date("t") will give you the number of days in this month; add it to 730 (two years worth of days). The hard part will be determining if April 29 exists in your two-year span. date("L") gives you a 1 or a 0 if a year is a leap year. The question is does April of a leap year even come into play?

Hopefully this helps.

"In theory, there's no difference between theory and practice. In practice, there is." -- Yogi Berra

He has: 46 posts

Joined: Jun 2004

Hopefully a blend of KarenArt's solution and mine should give you the answer. (Yeah, it really did take me 10+ minutes to complete my post. I was in the middle of one train of thought and had a better idea.)

"In theory, there's no difference between theory and practice. In practice, there is." -- Yogi Berra

He has: 1,758 posts

Joined: Jul 2002

Thanks guys. You've given me something to think about. I was trying to achieve something based on a little code snippet I found on the php.net site and it nearly worked (it does allow for leapyears allready - god knows how).

I just want it to always start from the beginning of the current month rather than "today" and it just doesnt seem to want to do it. grrrr.

Andy

KarenArt's picture

She has: 354 posts

Joined: May 2001

I think you may be having that problem because you're telling it to count days starting with today.
Perhaps if you tell it to count months for each year and then tell it how many days are in each month, that might be what you want.

Best of luck with it!
Calendars can be so frustrating to make sometimes. Wink

gotta finish redesigning my sites so I can show them again.

The purpose of education is... to get more jokes!

He has: 46 posts

Joined: Jun 2004

OK, I think I've got it. The first time you execute the line:

$my_time = strtotime("+$i DAY");

in the for loop, $my_time takes today's date as the default value. Outside of the loop you need to set $my_time to the first of the current month. This should work:

$my_time = strtotime("1 " . date('F Y'));

If I'm right you'll end up with $my_time being strtotime("1 August 2004"). Then when you hit the for loop you'll add 0 days. That should take care of what date the script starts with. I hope (fingers crossed).

"In theory, there's no difference between theory and practice. In practice, there is." -- Yogi Berra

He has: 1,758 posts

Joined: Jul 2002

Thanks for the help guys, I've decided to start again from scratch with my own code because I can't get my head round the code I nicked! Wink

Plus it's going to require lot's of updating in the future so I'm better off understanding what I'm doing than trying to bodge it!

Andy

He has: 1,758 posts

Joined: Jul 2002

Yay! I got it working exactly as I wanted. Thanks for all your suggestions guys your help has been invaluble and I really appreciate it! (especially the thing about leap years! that worked a charm!).

You can see it functioning at:
http://www.greendezire.com/calendar.php

I'll be doing some more work on it soon to turn it into something really useful but you guys have got me started and for that I'm really thankful!

Andy

He has: 46 posts

Joined: Jun 2004

Glad to hear you got it working. Congratulations. Yeah, sometimes if you just can't figure out what the code is doing you're better off writing your own. Sometimes you even stumble upon a function you've never heard of before.

I just checked out the link and nothing shows up. Looking at the source there an opening tag and a tag, then lots of nothing until the tag. Also, the and tags are blank. Does that have anything to do with the fact that I'm using IE6? (That's all I can use at work.)

Anyway, congratulations again, and I'm glad I could be of help.

"In theory, there's no difference between theory and practice. In practice, there is." -- Yogi Berra

He has: 1,758 posts

Joined: Jul 2002

sorry... I've expanded the script since then. You need to append ?id=1 or ?id=2 to the end of the script. (it's calling extra info from a db and I haven't yet told the script what to do if the id hasn't been specified, hence the weiredness).

http://www.greendezire.com/calendar.php?id=1
http://www.greendezire.com/calendar.php?id=2

Andy

KarenArt's picture

She has: 354 posts

Joined: May 2001

Hey congratulations!
Making a calendar work... you're good. Laughing out loud

It looks terrific!
So will you share the code with us or will we have to pay for it? Wink

Have fun with it!

gotta finish redesigning my sites so I can show them again.

The purpose of education is... to get more jokes!

He has: 1,758 posts

Joined: Jul 2002

Hopefully if I ever get it finished It's going to be a free web based service that anyone will be able to use to display a calendar on their website.

Andy

He has: 46 posts

Joined: Jun 2004

It really looks great, Andy. Nice job.

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.