• Home
  • About
  • Contact
  • Advertise

Devlounge

Design, Develop, and Grow

ateşli sevgilisi fantezisini yaşamak istediğini söyler porno ona arkadaşları varken onunla gizli saklı seks yapmak istediğini sikiş söyleyen kız hafta sonu için gelen arkadaşının görmediği bir sikiş açıdan sakso çekmeye başlayınca adamın yarağını bu altyazılı porno şekilde indiremez ve açık şekilde salonda sikişimeye sex izle başladıklarında misafir kızı da bu sekslerine rokettube konuk ederler seks yapacağını düşünmeyerek onun sex izle oyun oynadığını zanneder sabah olur ve herkes uyanır hd porno bu sırada yanında şişme mankenini de getiren sapık erotik hikayeler genç sınav haftası ders çalışan genç adam üvey annesinin sikiş eve gelmesiyle hayatının şokunu yaşar

  • Home
  • Code
  • Design
  • Design Focus
  • Interviews
  • Publishing
  • Strategy
  • Webapps
  • Extras

Create a Simple Calendar Using PHP

January 7, 2009 By Gerald Hanks

Here’s a simple, useful calendar script. This is a great way to check on days of the week for birthdays, holidays and other important occasions. You can also use it in conjunction with a data file (e.g. .csv, .xml) or database table (e.g. MySQL) as a tool to track appointments and special events.

This particular script makes many uses of the PHP date formatting functions. For more information on how PHP date formating works, see the PHP manual (http://us3.php.net/date).

First off, check to see if the user has sent previous values for the month and year via the form or the query string.
If this is the initial load of the form, then the system will pull the current month and year.
The “isset” function checks for the existence of the $_GET (query string) variables.

[php]
<!–//if values are not set for $Month and $Year, get current date values
if ((!isset($_GET[“Month”])) && (!isset($_GET[“Year”]))) {
$Month = date(“m”);
$Year = date(“Y”);
}
else{
$Month = $_GET[“Month”];
$Year = $_GET[“Year”];
}
[/php]

Now, we use the month and year values to get the Unix timestamp:

[php]
//Get viewed month
$Timestamp = mktime(0,0,0,$Month,1,$Year);
[/php]

We can use the date formatting function to get the full month name (e.g. January, February, etc.):

[php]
$MonthName = date(“F”, $Timestamp);
[/php]

We use the “echo” function to print the start of the table to the screen:

[php]
//Make calendar table
echo(“Calendar Table for $MonthName, $Year
“);
echo(

“);
echo(”

“);
[/php]

We create the array for the days of the week and loop through the array elements to fill the table headers:

[php]
//loop through days
$daysOfWeek = array(“Sun”,”Mon”,”Tue”,”Wed”,”Thu”,”Fri”,”Sat”);
foreach ($daysOfWeek as $value) {
echo(”

“);
}
echo(“

“);
[/php]Here is another date formatting function that we can use to get the day of the week for the first day of the month. Remember, this is a zero-indexed array. Thus, 0=Sunday, 1=Monday, etc.

[php]
//get day of week at start of month
$MonthStart = date(“w”, $Timestamp);
if ($MonthStart == 0) { // if the month starts on a Sunday
$MonthStart = 7;
}
[/php]

Now we get the last day of the month by “0th” day of the next month:

[php]
//get last day of month
$LastDay = date(“d”, mktime(0,0,0,$Month+1, 0, $Year);
[/php]

Another option is to find the number of days in the current month. Using this option will affect the calculations further down the routine. If you like, you can use this option and adapt the rest of the routine to this new setting.

[php]
//$LastDay = date(“t”, $Month);
[/php]

Get the number of “blank” entries at the start of the month by using the day of the week that starts the month.

[php]
//get start date
$StartDate = -$MonthStart;
[/php]

Now we can start filling the table cells for the calendar.

First, the we set up six rows for up to six weeks:

[php]
for ($k=1;$k<=6,$k++){ //print six rows for six possible weeks
echo(“

“);
[/php]

Then, we set up the columns for the seven days of the week:

[php]
for ($j=1;$j<=7;$j++){ //seven columns per row
[/php]

The system will add 1 to the $StartDate variable for each day in the week. From there, we test the value of the variable. If the $StartDate variable is either before the start of the month or after the end of the month, the calendar shows a blank space. If the $StartDate variable is within the number of days of the month, the calendar prints the date.

[php]
$StartDate++;
if(($StartDate $LastDay) { //blank calendar space
echo(”

“);
}
elseif (($StartDate <=1 && ($StartDate <= $LastDay)) { //date goes here
echo(”

“);
}
}
[/php]At the end of the week, we end the table row and start a new week.

[php]
echo(“

“);
} //End Table Row
[/php]

After we’ve finished all six weeks, we end the table.

[php]
echo(“

$value
$StartDate

“);
[/php]

That takes care of the current month. Now what do we do if we want to see another month?

Here’s how we create a form that allows a user to select a different month:

[php]
//create select form

echo(”

“);
echo(“Select a new month to view:
“);
[/php]

Instead of listing the months individually, we can use the date formatting function to create a dynamic loop and display the month names:

[php]
echo(“”); for($m=1;$m<=12;$m++){ echo(“” .date(“F”, mktime(0,0,0,$m,1,$Year)) . “”); } echo(“”);
[/php]

We can do the same for the year (2008-2020):

[php]
echo(“”); for($y=2008;$y<2020;$y++){ echo(“$y”); } echo(“”);
[/php]

Now we finish the form with the submit button:

[php]
echo(“”);
echo(“”);

[/php]

Again, this is a simple yet functional calendar script. If you’d like to see how to add other features to this calendar, such as flat file or database support, please let us know.

Filed Under: Code Tagged With: calendar, mysql, PHP

Code & Tutorials

Which Front-End Development Languages Will Grow in 2017?

Your Guide to Leveraging APIs as a Developer

Bitcoin Processing Website Integration For Web Developers

Website Security For 2016 That All Developers Need To Know

5 Reasons You Need to Be Using jQuery

About Devlounge

Want to read more about Devlounge, or maybe you want to contact us, or even advertise? Oh, and don't forget to subscribe to updates!

The Best of Devlounge

How to Personalize Forms with CaptainForm

Search