Solved

Calandar in PHP

Posted on 2009-04-02
22
260 Views
Last Modified: 2012-05-06
Hello,

I have a calendar on my website: http://www.personally-yours.co.uk

I would like to have the calandar script update once per month so that it is on less thing for me to update every month as I forget to do this.

Is there anyway this can be done.

The code for the calendar is below.  If you have a solution would you please post the entire code as well.

Thanks.
<?php
 

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)).'&nbsp;'.$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>&nbsp;'; 

    if($n) $n = '&nbsp;<span class="calendar-next">'.($nl ? '<a href="'.htmlspecialchars($nl).'">'.$n.'</a>' : $n).'</span>'; 

    $calendar = '<table class="calendar">'."\n". 

        '<caption class="calendar-month">'.$p.($month_href ? '<a href="'.htmlspecialchars($month_href).'">'.$title.'</a>' : $title).$n."</caption>\n<tr>"; 
 

    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.'">&nbsp;</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).'" title="Easter">'.$content.'</a>' : $content).'</td>'; 

        } 

        else $calendar .= "<td>$day</td>"; 

    } 

    if($weekday != 7) $calendar .= '<td colspan="'.(7-$weekday).'">&nbsp;</td>'; #remaining "empty" days 
 

    return $calendar."</tr>\n</table>\n"; 

} 

?>

<?php 

$time = time(); 

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

$days = array(

        $today=>array(NULL,NULL,'<span style="color: red; font-weight: bold; font-size: larger; text-decoration: blink;">'.$today.'</span>'),

		12=>array('http:\\www.personally-yours.co.uk\letters.php',NULL,'<font color="#00AA00" style="font-weight:bold; font-size:larger; text-decoration:blink">12</font>')

		); 

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

?>

Open in new window

0
Comment
Question by:myth3_16
22 Comments
 
LVL 10

Expert Comment

by:ollyatstithians
ID: 24050813
Surely your script will update each time the page is requested? Or am I missing something.
On the subject of running scripts on a timed basis, you may find your hosting provider offers a "cron jobs" service (or equivalent scheduler). This should allow you to run an update script.

Olly.
0
 
LVL 36

Expert Comment

by:Loganathan Natarajan
ID: 24051049
0
 

Author Comment

by:myth3_16
ID: 24051157
I think you miss my meaning.

On the calendar at the moment there is a green number, this number is sunday 12th April which is easter Sunday.  This number is linked to another page.

What I want is without any interaction from me, I would like the calendar to display the important date in that calendar month.

At the moment I have to update the calendar.php file every month and sometimes i miss the first of the month so when the month turns over it automatically displays the important dates within this month.

Is this possible?
0
 
LVL 10

Expert Comment

by:ollyatstithians
ID: 24052222
Ah! Yes.
You will need to provide a list of important dates. This could be from a database, or even just an array in the script or an included file.
If you make the array indexes timestamp numbers, you can supply the script with special dates pretty much forever.
Then you will need to modify the calendar script slightly to accept the timestamp indexes.
I'll have a fiddle with your script and post again later.

Olly.
0
 
LVL 10

Expert Comment

by:ollyatstithians
ID: 24052284
Here you go!

Olly.
<?php

 

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">'."\n". 

        '<caption class="calendar-month">'.$p.($month_href ? '<a href="'.htmlspecialchars($month_href).'">'.$title.'</a>' : $title).$n."</caption>\n<tr>"; 

 

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

        }

				$index = strtotime(str_pad($year, 4, '0', STR_PAD_LEFT).str_pad($month, 2, '0', STR_PAD_LEFT).str_pad($day, 2, '0', STR_PAD_LEFT)); // Changed here to accept timestamp indexes

        if(isset($days[$index]) and is_array($days[$index])){ // and here

            @list($link, $classes, $content) = $days[$index]; // and here

            if(is_null($content))  $content  = $day; 

            $calendar .= '<td'.($classes ? ' class="'.htmlspecialchars($classes).'">' : '>'). 

                ($link ? '<a href="'.htmlspecialchars($link).'" title="Easter">'.$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"; 

} 

?><?php 

$time = time(); 

$today = strtotime(date('Ymd', $time)); // Changed

$days = array(

	$today=>array(NULL,NULL,'<span style="color: red; font-weight: bold; font-size: larger; text-decoration: blink;">'.date('j',$today).'</span>'), // added date() to turn timestamp back into day number

	strtotime('20090412')=>array('http:\\www.personally-yours.co.uk\letters.php',NULL,'<font color="#00AA00" style="font-weight:bold; font-size:larger; text-decoration:blink">12</font>') // Changed to add strtotime() to generate timestamp.

	); 

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

?>

Open in new window

0
 

Author Comment

by:myth3_16
ID: 24052464
So when this rolls over to other months then where do I specify the other dates throughout the year including may so that I don't have to update it every month.
0
 
LVL 10

Expert Comment

by:ollyatstithians
ID: 24052558
Near the bottom of the script there is an array call $days. Just add extra items to that. Just be sure to add them in the same way as I have: that is use strtotime() to make the key a timestamp.
$days = array(

  $today=>array(NULL,NULL,'<span style="color: red; font-weight: bold; font-size: larger; text-decoration: blink;">'.date('j',$today).'</span>'), // this is the highlight for TODAY

  strtotime('20090412')=>array('http:\\www.personally-yours.co.uk\letters.php',NULL,'<font color="#00AA00" style="font-weight:bold; font-size:larger; text-decoration:blink">12</font>'), // This is a special day

  strtotime('20090413')=>array('http:\\www.personally-yours.co.uk\letters.php',NULL,'<font color="#00AA00" style="font-weight:bold; font-size:larger; text-decoration:blink">12</font>') // This is another special day

        ); 

Open in new window

0
 
LVL 10

Expert Comment

by:ollyatstithians
ID: 24052567
oops. except I didn't change the '12' label. Sorry!
$days = array(

  $today=>array(NULL,NULL,'<span style="color: red; font-weight: bold; font-size: larger; text-decoration: blink;">'.date('j',$today).'</span>'), // this is the highlight for TODAY

  strtotime('20090412')=>array('http:\\www.personally-yours.co.uk\letters.php',NULL,'<font color="#00AA00" style="font-weight:bold; font-size:larger; text-decoration:blink">12</font>'), // This is a special day

  strtotime('20090413')=>array('http:\\www.personally-yours.co.uk\letters.php',NULL,'<font color="#00AA00" style="font-weight:bold; font-size:larger; text-decoration:blink">13</font>') // This is another special day

        ); 

Open in new window

0
 
LVL 10

Expert Comment

by:ollyatstithians
ID: 24052583
The advantage being that you can update it whenever you want with events for the next few hundred years (not sure when the timestamp runs out).

Olly.
0
 

Author Comment

by:myth3_16
ID: 24052729
Cheers My friend!!!!

You're a star.

I have another thing on my site too, product of the month.

This needs to be changed once a month too, i'm going to post a new post about it.

the code for it is below:

Cheers for your help
<div align="center"><?

$username="***********";

$password="********";

$database="**************";
 

mysql_connect("localhost",$username,$password);

@mysql_select_db($database) or die( "Unable to select database");

$query="SELECT img_url, product_name, url FROM product WHERE img_url='images/mainbody/easterbunny.png'";

$result=mysql_query($query);
 

$num=mysql_numrows($result);
 

mysql_close();
 

$i=0;

while ($i < $num) {
 

$itemname=mysql_result($result,$i,"product_name");

$imgurl=mysql_result($result,$i,"img_url");

$url=mysql_result($result,$i,"url");
 

echo "<a href=$url><img src=$imgurl width=123 height=150 alt=$itemname></img></a><br>$itemname<br><br><a href=$url>Click Here For More Info</a>";
 

$i++;

}

?><br /></div>

Open in new window

0
 
LVL 10

Expert Comment

by:ollyatstithians
ID: 24052849
Doesn't look like a problem. Maybe try a few more points next time eh? :D

I'll have a look tomorrow.

Olly.
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:myth3_16
ID: 24052919
More points added
0
 
LVL 3

Expert Comment

by:FirestormX
ID: 24053337
Not to steal points away from ollyatstithians (all the points should still go to him, since he answered the question; I'm just copying what he's already done)

I'll copy your code from line 7 and 8 (your query), and add the array (the same way olly had it) at the top.  The index will be the month, followed by the year, and then in the query, we'll use date() to find the current month and year.
$products[042009] = "images/mainbody/easterbunny.png"; 

$products[052009] = "images/mainbody/mayflowers.png";

$products[062009] = "images/mainbody/school_out.png";
 

//Now line 7 and 8 of your code, with the modified "WHERE img_url="

$query="SELECT img_url, product_name, url FROM product WHERE img_url='".$products[date('mY')]."'";

$result=mysql_query($query);

Open in new window

0
 

Author Comment

by:myth3_16
ID: 24054308
it isn';t showing, the product of the month box is empty.  Can you help firestorm????
0
 

Author Comment

by:myth3_16
ID: 24055138
I am getting a picture displayed now but it is the wrong pic.  The being displayed is the fnparch_style1.png whereas for the month of april it should be easterbunny.png

This is my code now:

<div align="center"><?

$username="personallyyours";

$password="austin316";

$database="brettrogers_dbpersonallyyours";
 

mysql_connect("localhost",$username,$password);

@mysql_select_db($database) or die( "Unable to select database");

$products[200904] = "images/mainbody/easterbunny.png"; 

$products[052009] = "images/mainbody/dayborn.png";

$products[062009] = "images/mainbody/nuparch_style1.png";

$products[072009] = "images/mainbody/35mmx24mmKeyring(Bf1)cancer.png";

$products[082009] = "images/mainbody/90mmx90mmCoaster(N1).png";

$products[092009] = "images/mainbody/fnparch_style1.png";

$products[102009] = "images/mainbody/50mmx35mmFridgeMagnet(C1)aries.png";

$products[112009] = "images/mainbody/marriage3.png";

$products[122009] = "images/mainbody/santaletter.png";
 

//Now line 7 and 8 of your code, with the modified "WHERE img_url="

$query="SELECT img_url, product_name, url FROM product WHERE img_url='".$products[date(Null,'Ym')]."'";

$result=mysql_query($query);
 

$num=mysql_numrows($result);
 

mysql_close();
 

$i=0;

while ($i < $num) {
 

$itemname=mysql_result($result,$i,"product_name");

$imgurl=mysql_result($result,$i,"img_url");

$url=mysql_result($result,$i,"url");
 

echo "<a href=$url><img src=$imgurl width=123 height=150 alt=$itemname></img></a><br>$itemname<br><br><a href=$url>Click Here For More Info</a>";
 

$i++;

}

?><br /></div>

Open in new window

0
 
LVL 10

Expert Comment

by:ollyatstithians
ID: 24057612
Line 33: You missed the all the quotes from your <img> tag. Also the closing </img> is better written into the opening tag.

Try this.

Olly.
echo "<a href=\"$url\"><img src=\"$imgurl\" width=\"123\" height=\"150\" alt=\"$itemname\" /></a><br>$itemname<br><br><a href=\"$url\">Click Here For More Info</a>";

Open in new window

0
 

Author Comment

by:myth3_16
ID: 24057705
This is all well and good but i'm still getting the wrong pic displayed.
The pics that should be displayed should be the one from the array
$products[200904] = "images/mainbody/easterbunny.png"; (april)

At the moment it is displaying the pic from the array
$products[092009] = "images/mainbody/fnparch_style1.png"; (september)

Help!!
0
 
LVL 10

Accepted Solution

by:
ollyatstithians earned 250 total points
ID: 24057917
Your date() function has a NULL in line 19 and no current timestamp.

$query="SELECT img_url, product_name, url FROM product WHERE img_url='".$products[date(Null,'Ym')]."'";

should be

$query="SELECT img_url, product_name, url FROM product WHERE img_url='".$products[date('Ym', time())]."'";

See http://uk3.php.net/manual/en/function.date.php

Olly.
0
 

Author Comment

by:myth3_16
ID: 24057988
That seems to hav egot it working, the correct pic is being diaplyed now, lets hope that when it turns to May the other pic is displayed.

Thanks for all your help!!!!

Your A Star and you now have 250 points!
0
 
LVL 10

Expert Comment

by:ollyatstithians
ID: 24058027
You can test it for different months by swapping the time() function with another timestamp.

Olly.
0
 

Author Comment

by:myth3_16
ID: 24058093
what do you mean another timestamp?

0
 
LVL 10

Expert Comment

by:ollyatstithians
ID: 24058134
Make one with:

strtotime('20090507');

Olly.
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

Introduction HyperText Transfer Protocol (http://www.ietf.org/rfc/rfc2616.txt) or "HTTP" is the underpinning of internet communication.  As a teacher of web development I have heard many questions, mostly from my younger students who have come to t…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now