PHP Diary | PHP Housekeeping | PHP Scripts | TD Scripts.com
[goto prior diary entry]
12/18/99 "Inserting Dates"
[goto next diary entry]
I added content to PHP Housekeeping, so that you could follow along better with the various symbols and code that I use to demonstrate how I am learning this language. You may want to pop over to that page really quick.
As you can see from the title of this diary entry, I still haven't done any actual PHP coding (yet). I am eager to get down to it, so let's prepare the php-enabled diary page. You are probably familiar with HTML tags. the one to insert to start our PHP coding is:
<?
inside here is where all the PHP coding goes. Now to end the coding we type:
?>
Thus to insert the date in the HTML page below (repeated from yesterday):
<? print(date("1 F d, Y")); ?>
This should ouput in the format:
Saturday December 18, 1999
To actually do this we need to create an HTML page and save it with the file extension .php3 so that the server knows it is a php-enabled page. This again is similar to Server Side Includes because those pages usually need to be saved as .shtml. I assume most of you are familiar with how to create an HTML page, but if you aren't view the source of my very first php effort and you'll see how it is done. Btw, most programming things start out with the rather useless, "Hello World" example. I think messing around with dates are more fun. Ok, I'm nervous...let's see if it works, because I see something already that I am curious about ...
example #1 TDavid's first ever PHP example
BUG! Instead of Saturday it says "1" -- that is because the code should be a lowercase L not the number 1. I changed that for example #2.
example #2 changed the number 1 to the lowercase letter L
Now that looks right! But ... I'd like to mess around with the format some more to get the month/day/date format like I do for the diary date. If we refer to the date codes sheet from 12/17/99 and in the php downloadable manual I see that the codes will be:
n - month without leading zeros; i.e. "1" to "12"
j - day of the month without leading zeros; i.e. "1" to "31"
y - year, 2 digits; i.e. "99"
I noticed on the day of the month that I can choose to have leading zeroes if I use the lowercase d date code, but I decided I'd rather go without the leading zeroes and use the j date code. Now I need to insert those slashes into the code. I am not sure how this will work, but let's check it out. Since it printed my 1 in example #1, my assumption would be you can insert other non-date codes in with the code and it will just print them out. Let's see how the code will look:
<? print(date("m/j/y")); ?>
example #3 current month/day/date format
Ok, we're close to getting it the way I want, but we still need to make the fuschia color and proper font (Arial regular size) so it looks like this: 12/18/99. First we do our HTML, then jump into PHP to print the current date, and then jump out and end the HTML <font> tags. The code should look like this:
<font face="Arial" color="#FF00FF"><strong>example #4 current month/day/date format - with HTML colors and formatting
Obviously I don't want the current date inserted in place of my diary date - but this shows how I could insert a current date rather easily into a PHP page. I want to take this concept further and make it a little more practical.
Since I am interesting in developing dynamic (also known as realtime) applications using PHP, I'd like to have PHP display different things based on the day of the week. This would keep my page looking fresher and save me time as well, since I wouldn't have to manually FTP changes to my page when I wanted it changed.
This requires the introduction of the if statement in PHP. if and else statements are a basic fundamental of programming. In referencing the PHP manual I realized a somewhat significant difference between the Perl version of elsif and elseif in PHP. That missing "e" that has caused me many 500 internal server errors in Perl CGI scripts has been "put back" in PHP, thank goodness :)
Conditional if statements work essentially like this:
if (something
comparedto somethingelse) {
do this
} elseif
(something comparedto somethingelse) {
then do
this instead
}
} else {
then neither of the above apply so do this
}
I will be continue these thoughts in my next lesson.
[goto prior diary entry]
12/18/99 "Inserting Dates"
[goto next diary entry]
PHP Diary | PHP Housekeeping | PHP Scripts | TD Scripts.com
Copyright 2000 php-scripts.com