The following course text material is Copyright KMR Enterprises All Rights Reserved. You are welcome to download and copy the course text material and use for your own personal study/use. You are not permitted to post, publish and/or redistribute the course text in any other format, online or offline without express written permission from KMR Enterprises. You are welcome to and encouraged to share your own course to-do assignments online (your blog, website, etc) and link back to the relevant course material here at Webmaster Cookbook. Thank you and happy coding to you!
Lesson #2a: Working with Date & Time and using conditionals
Review
Welcome to the second of sixteen lessons on PHP 101 programming. In lesson #1
we learned what tools are needed to write PHP scripts, and how to store and print/echo
strings containing character or numbers to the browser. This week we are going to create our first useful script: a picture of the day script. Therefore every day of the week a surfer will be greeted by a different picture. If you have never done any programming before, I strongly urge you to go back and read through and start with the first lesson.
Date
The pic of the day part involves the date, so we need to explore how to use PHP to get the current server date. You should understand first that all dates work inherently off of the server date. Server date is not the same, of course, as your local date. You may live on the west coast, for instance, and use a hosting facility on the east coast. Or live in UK and use a hosting place in USA. We get the date by using the php date function. If you consult your php reference manual (see last week tutorial for download instructions) you will see the function definition looks as follows:
string date(string format, int [timestamp]);
This is translated to mean: to print or store the date use the date function and enclose in parenthesis the format you want to output the date into. For example let's say we wanted to print to the browser:
The current date is: 9/14/05
As explained in lesson #1, you will almost always want to break down each programming task into smaller bite-sized pieces, if you will. Therefore, first you would consult the following string format table in php reference manual to find the date codes you needed to format the current server date:
These are the string format codes to use when determining the date format. So let's break down what we need in the order from left to right. First we need the day in single digit format (no trailing zeroes), then the month in single digit format, and finally the year without the century (20) in front.
j - day of the month without leading zeros; i.e. "1" to "31"
n - month without leading zeros; i.e. "1" to "12"
y - year, 2 digits; i.e. "99"
You should note that some of the date codes above use UPPERCASE instead of lowercase, and yes it does make a difference when you want to create the date format. Now that we have the correct date codes let's build the PHP code to print what I showed you above.
<?php
print("The current date is: ");
print(date("j/n/y"));
?>
Adding strings together is called concatenation
While the above code will do what we want, there is a more efficient way to make both strings into one and avoid using multiple print functions for a single line. This is called concatenation or concatenating strings which means to add the two strings together to make one. If we had used the code print("The current date is: date("j/n/y"); this would have produced an error because for one, we wouldn't have matching opening and closing parenthesis and two, PHP would have treated the words like string text and not a PHP function. Here is how the same code above would look using concatenation:
<?php
print("The current date is: " . date("j/n/y"));
?>
Bug Squashing Tip: It is a fairly common error to forget/omit opening or closing parenthesis, fortunately PHP will output the line number of the error(s) so you can more easily spot the bug. No programmer I've ever met or known writes flawless code every time out, so dealing with bugs is a necessary part of the coding process.
There is another way I want to show you how to produce the same output. It will illustrate how to store the contents of the current date into a string. Let's use the string $today as a container and then print it to the browser.
<?php
$today = date("j/n/y");
print("The current date is: $today");
?>
You may notice that we did not need to use the period nor the extra parenthesis inside the print function to create the same output. If we needed to manipulate the string $today somewhere in our script, this would be the best way to code it. Now before we can get back to the pic of the day script we need to understand conditionals.
Multiple concatenation
To keep your code display neater you can also use concatenation as follows. Note that the first assignment of the $output variable and the .= means "add to the end of the last variable assignment:
<?php
$output = 'The current';
$output .= ' date is ' ;
$output .= date("j/n/y");
echo("$output");
?>
Conditionals - if this happens do this, elseif this happens we do this, otherwise (else) we must do this ...
Just as using and storing information in strings and printing is a vital basic function of programming, so are conditionals. Sooner or later you will need to do things based on an outcome. In our picture of the day script we need to know what day it is and then react according to that. If it is Wednesday, for example, we don't want the script to print the picture of the day for Friday. Fundamentally, this is how a conditional works. Let's look at a visual representation of the syntax of if elseif and else statements:
if (something
compared_to somethingelse)
{
do this
} elseif (something
compared_to somethingelse)
{
then do
this instead
}
} elseif (something
compared_to somethingelse)
{
then do
this instead
}
} else {
then none of the above apply so do this
}
You will notice the use of { brackets } to contain the branches of code. Just as you must have equal numbers of opening and closing parenthesis, you must have equal numbers of brackets. You can include very simple one line instructions of code (see below) to very complex sections of code which do many different things based on a condition. The parenthesis are used to compare the left side to the right side of a condition. These are the basic conditional (also known as logical and relational) operators and their meanings:
== is equal to
!= is NOT equal to
< is less than
> is greater than
<= is less than or equal to
>= is greater than or equal to
Using conditionals we can now create a simple, yet effective PHP script to print a different picture based on the day of the week.
<?php
$today = date("l");
if ($today == "Sunday")
{
print ("<img src=\"image0.jpg\">");
}
if ($today == "Monday")
{
print ("<img src=\"image1.jpg\">");
}
if ($today == "Tuesday")
{
print ("<img src=\"image2.jpg\">");
}
if ($today == "Wednesday")
{
print ("<img src=\"image3.jpg\">");
}
if ($today == "Thursday")
{
print ("<img src=\"image4.jpg\">");
}
if ($today == "Friday")
{
print ("<img src=\"image5.jpg\">");
}
if ($today == "Saturday")
{
print ("<img src=\"image6.jpg\">");
}
?>
Power Tip: One new thing was introduced above and that was backslashing \ the doublequote in the <IMG> HTML tag. If we didn't do this PHP would bark because the open and closing double quotes inside the print function determine the contents of the string. Forgetting to backslash HTML double quotes is a common PHP programming error.
Type the script into your text editor and save it with the name picday.php. This illustrates how to combine and store date in strings, conditionals, print strings using HTML to create a useful script that will dynamically (in real server date and time) print a different image based on the day of the week.
This concludes Part A of Lesson #2 of the PHP 101 scripting course. Next lession - 2B | HomeCopyright 2000-2008 KMR Enterprises All Rights Reserved