Note: you must have calendar support enabled on php for this to work. you can check this with phpinfo();This tutorial was last edited Sunday 19th February
Making a calendar in PHP is quite simple, you just need the right logic.
We'll start off with declaring our variables and starting off the function.
| function createCalendar() { $calendar = "CAL_GREGORIAN"; // Sets the "int" argument for the calendar functions. $year = date("Y"); // This year $unix = time(); // Now $today = unixtojd($unix); // Converts into Julian $today = cal_from_jd($today, CAL_GREGORIAN); // Converts into normal (i.e. weekday) |
The comments should help you out. Now that we've done that, we're going to start the output.
| echo "<table cellpadding="3" cellspacing="0">"; echo "<tr><td class="headdate" colspan="33" align="left"><b>Welcome, {$_SERVER['REMOTE_ADDR']}. | Today is ".$today[date]."</b></th></td>"; |
And now we finish off the output:
| $months = array( "January" => 1, "February" => 2, "March" => 3, "April" => 4, "May" => 5, "June" => 6, "July" => 7, "August" => 8, "September" => 9, "October" => 10, "November" => 11, "December" => 12 ); // Sets up the months into an associative array. $r = 0; foreach($months as $key => $value) { $days_in_month = cal_days_in_month(CAL_GREGORIAN,$value,$year); // How many days there are in the month echo "<tr style=""; // The next few lines are simply for aesthetics. if($key==date("F")) { echo " background: #239487;"; } echo "cursor: default;""; if($r==1) { echo " bgcolor="#CFCFCF""; $r = 0; } else { $r++; } echo "><th><b>$key $year</b></th>n<td>"; for($i=0;$i<$days_in_month;$i++) { $week_number = date("Week W",mktime(0, 0, 0, $value, ($i+1), $year)); // Finds out the week of the year $weekday = date("l",mktime(0, 0, 0, $value, ($i+1), $year)); // What weekday it is today echo "<td title="$week_number: $weekday"><a href="?">"; if($weekday==date("l") & & $i == (date("j")-1)) { echo "<!-- This is today -->"; } if($weekday=="Saturday"||$weekday=="Sunday") { // Is it a weekend? echo "<b>".($i+1)."</b> "; } else { echo ($i+1)."</a></td>n"; } } } echo "</td></tr>"; echo "</table>"; } |
The full function:
| function createCalendar() { $calendar = "CAL_GREGORIAN"; // Sets the "int" argument for the calendar functions. $year = date("Y"); // This year $unix = time(); // Now $today = unixtojd($unix); // Converts into Julian $today = cal_from_jd($today, CAL_GREGORIAN); // Converts into normal (i.e. weekday) echo "<table cellpadding="3" cellspacing="0">"; echo "<tr><td class="headdate" colspan="33" align="left"><b>Welcome, {$_SERVER['REMOTE_ADDR']}. | Today is ".$today[date]."</b></th></td>"; $months = array( "January" => 1, "February" => 2, "March" => 3, "April" => 4, "May" => 5, "June" => 6, "July" => 7, "August" => 8, "September" => 9, "October" => 10, "November" => 11, "December" => 12 ); // Sets up the months into an associative array. $r = 0; foreach($months as $key => $value) { $days_in_month = cal_days_in_month(CAL_GREGORIAN,$value,$year); // How many days there are in the month echo "<tr style=""; // The next few lines are simply for aesthetics. if($key==date("F")) { echo " background: #239487;"; } echo "cursor: default;""; if($r==1) { echo " bgcolor="#CFCFCF""; $r = 0; } else { $r++; } echo "><th><b>$key $year</b></th>n<td>"; for($i=0;$i<$days_in_month;$i++) { $week_number = date("Week W",mktime(0, 0, 0, $value, ($i+1), $year)); // Finds out the week of the year $weekday = date("l",mktime(0, 0, 0, $value, ($i+1), $year)); // What weekday it is today echo "<td title="$week_number: $weekday"><a href="?">"; if($weekday==date("l") & & $i == (date("j")-1)) { echo "<!-- This is today -->"; } if($weekday=="Saturday"||$weekday=="Sunday") { // Is it a weekend? echo "<b>".($i+1)."</b> "; } else { echo ($i+1)."</a></td>n"; } } } echo "</td></tr>"; echo "</table>"; } |
Related Articles
- When Does an Expired Domain Name Become Available?In order to know exactly when an expiring domain name will become available for re-registration by a new owner, you must understand the
- Common Sections for Your WebsiteIve focused in the past on some individual sections of your website that you should consider implementing. What Im going to do now is give a brief overview of a number of pages and sections that are common in websites.
- The Critical Coupon Code StepAffiliate marketing is becoming as competitive (and more affordable) as Pay Per Click marketing, Media Buys, email and other methods of marketing online. It takes more ingenuity and commitment from the merchants to make their affiliate program successful and profitable.
- Update Your Site Instantly With SSIThere are probably two main ways you currently update your site. The first, and most important, is adding new content. Whether you are updating a calendar, adding a new page, or putting in a photo of a new product, you are enhancing the value of your site for your visitors. The second, and more t...
- Take This Blog And...Youve decided your company will start a blog. Youve developed a strategy, picked and configured a platform, set a few general goals and put someone in charge. You are off to a great start.
- Search Engine Index Flux and Rank CheckingLike so many other search engine optimization agents, we, too, have found all ranking checkers we tested, including the market leaders, to be highly inaccurate at times, th...
- Option BoxesA lovely and well written bit of code that could be useful in so many ways. Two option boxes side by side that allow the user to select options in the first box and move them to the Your Choices box next to it
- Offers that Turn Lookers into BuyersIf youre getting only a sluggish response for a product or service that people genuinely need, wake buyers up by spicing up your offer. Ive seen losing propositions ...
- About Web ApplicationsWeb-based Applications are web sites that offer more to the user than normal, static web sites. The difference is the way the information is provided to you. The site actually responds to the user, changing to provide the information the user wants to see...
- Site CoversSite covers can serve a number of different purposes. Some function as splash screens that offer little information, but are intended to entice users into a site by using snazzy graphics or effects. Others are used to establish an overall look-and-feel for a site...
