Web Developer Forum
June 12, 2024, 11:03:06 am
Welcome, Guest. Please login or register.

Login with username, password and session length
News: Please follow the rules to make our community helpfull and clean of swearing and other abusive content.
 
  Home Help Search Web Hosting & Domains Staff List Login Register  

[Example] PHP- Calendar - Example 01

Pages: [1]
  Print  
Author Topic: [Example] PHP- Calendar - Example 01  (Read 279 times)
Compuart
Moderator
Newbie
*
Posts: 22



View Profile
« on: September 01, 2009, 06:16:00 pm »


Let's make a calendar

While this style of tutorial will be fairly new, it will help readers apply what they have learned through the previous tutorials.  Tutorials from this point on will consist of short pieces of code with examples and line-by-line explanations.

We're going to make a calendar because it's a very simple way of applying basic loops and if statements to code.  It will also help new programmers understand basic thought processes and techniques to coding.

There are a few things you should note when making a calendar.  First of all, every month has a certain amount of days.  In English, the rule is "Thirty days hath September, April, June, and November".  The rest of the months all have thirty-one days except for February which has either 28 or 29 days depending on the year.  This isn't the biggest obstacle we have to face; the biggest obstacle is the fact that the beginning of each month starts on a different day (Sunday through Saturday as is the norm).

So let's start coding this thing and see what we can do.  The most important function we will use is the date() function (http://www.php.net/manual/en/function.date.php).  The date function will be able to get us all the information we'll need.

The script will have to know what month we are in:
Code:
$month = date('n');

It will also need to know the year:
Code:
$year = date('Y');

We will also need to know how many days are in the current month also, and we can use this to get that number:
Code:
$days_in_month = date('t');

We have some very basic information now, but we need some more information.  We need to know what day the current month starts on.  Our week starts on a Sunday and ends on a Saturday (which also happens to be how PHP works, so we can print that information out:

Code:
print "SU\tMO\tTU\tWE\tTH\tFR\tSA\n";

We used tabs in-between for uniform spacing.  It also helps us from having to work with calculating spaces to separate numbers.  To find what part of the week the first day of the month falls on, we can use the following code:

Code:
$first_day_of_month = date('w',mktime(1,1,1,$month,1,$year));

The code is very simple to understand.  "w" is the numeric representation of the day of the week, 0 (for Sunday) through 6 (for Saturday).  mktime() returns a Unix timestamp for a given date input.

int mktime ( [int hour [, int minute [, int second [, int month [, int day [, int year [, int is_dst]]]]]]])

so we simply tell PHP to give us the weekday of the first hour, minute, second, and day of the current month and year.  If that confuses you, try to match up the parameter list with the code above and it will help clear things up.  Up to now, we have all the basic information we need to build our calendar script:

Code:
$month = date('n'); //what month are we in?
$year = date('Y'); //what year are we in?
$first_day_of_month = date('w',mktime(1,1,1,$month,1,$year)); //get the first day of the month
$days_in_month = date('t'); //how many days in this month

Because we know the first day of the month doesn't start on the same weekday everyday, and we know how we will be representing our weekday ("su\tmo\ttu\twe\tth\tfr\tsa"), we have to somehow move ahead to the first day of the month, and begin counting from there all the way up to the last day of the month.

To do this, we can use a simple loop:
Code:
for ($spacer = 0; $spacer < $first_day_of_month; $spacer++) {
  print "  \t";
}

The loop counts from zero, right until before the weekday of the first day of the month.  We use two spaces and a tab to keep consistent with our weekday representations which consist of two letters and a tab.  We will subsequently make sure that our number outputs also have two digits and a tab.  This will line everything up.

Now that we've made enough spaces to know that we are at the first day of the month, we just have to keep looping until the last day of the month.

Code:
for ($x = 1; $x <= $days_in_month; $x++) {
  if ($spacer >= 7) { print "\n"; $spacer = 0; }
  if (strlen($x) == 1) { $x = "0$x"; }
  print "$x\t";
  $spacer++;
}

This code begins a loop starting from 1 to the number of days in the month ($days_in_month).  We will continue taking advantage of the $spacer variable we declared before.  We will use this to know when to go to the next line.  The second line in the above code manages when to go to the next line (this happens when we reach the end of the week, a Saturday).  When we reach the end of the week, we reset the $spacer counter to zero and go to the next line.  "if (strlen($x) == 1) { $x = "0$x"; }" takes care of spacing issues.  Because we want all of our output to be in double digits, we can use a simple statement to get double digits if we have single digits.

The final code with excessive commenting:

Code:
<?php

$day 
date('j'); //what day is it today
$month date('n'); //what month are we in?
$year date('Y'); //what year are we in?
//get the first first day of the month
$first_day_of_month date('w',mktime(1,1,1,$month,1,$year));
$days_in_month date('t'); //how many days in this month

print "SU\tMO\tTU\tWE\tTH\tFR\tSA\n"//print the weekday headers
//count ahead to the weekday of the first day of the month
for ($spacer 0$spacer $first_day_of_month$spacer++) {
  print 
"  \t";
}

for (
$x 1$x <= $days_in_month$x++) { //begin our main loop
  //if we have gone past the end of the week, go to the next line
  
if ($spacer >= 7) { print "\n"$spacer 0; }
  
//if the length of the current day is one, put a "0" in front of it
  
if (strlen($x) == 1) { $x "0$x"; }
  if (
$x == $day) { //is this day the current day
    
print "$x^\t"//if so put an indicator
  
} else {
    print 
"$x\t"//otherwise just print it
  
}
  
$spacer++; //increment our spacer
}

?>


We could have taken a few shortcuts here, but I wanted to use as many simple statements as possible.  The output of this script for the current month is:

Quote
SU   MO   TU   WE   TH   FR   SA
                              01   
02   03   04   05   06   07   08   
09   10   11^   12   13   14   15   
16   17   18   19   20   21   22   
23   24   25   26   27   28   29   
30   31   

Note that I added extra code to recognize what day in the month we are on.
Report Spam   Logged

Share on Facebook Share on Twitter

SkyDZines
Newbie
*
Posts: 3


View Profile
« Reply #1 on: September 04, 2009, 04:00:45 pm »

Very Nice, compuart...
Report Spam   Logged
Eliga66
Newbie
*
Posts: 3


View Profile
« Reply #2 on: January 07, 2012, 04:36:51 am »

Google Calendar allows client applications to view and update calendar events in the form of Google Data API feeds. Your client application can use the Google Calendar Data API to create new events, edit or delete existing events, and query for events that match particular criteria.
There are many possible uses for the Calendar Data API. For example, you can create a web front end for your group's calendar that uses Google Calendar as a back end. Or you can generate a public calendar for Google Calendar to display, based on your organization's event database. Or you can search relevant calendars to display a list of upcoming events on those calendars.
This document provides detailed examples for using the PHP Client Library to work with the Google Calendar service. You can download the client library as a standalone release (distributed by Zend) or as part of the Zend Framework. For help setting up the client library, see the Getting Started Guide. You will find examples of adding events, updating events, deleting events and querying events. If you're interested in understanding more about the underlying protocol used by the Zend Google Data APIs Client Library to interact with the Calendar Data API, please see the protocol tab.
Report Spam   Logged


Pages: [1]
  Print  
 
Jump to:  

Free Website Hosting
CO.CC:Free Domain
Bookmark this site! | Upgrade This Forum
SMF For Free - Create your own Forum

Powered by SMF | SMF © 2016, Simple Machines
Privacy Policy