Wednesday, July 02, 2008

How To Format The Current Date In PHP

While you may think that PHP is an acronym for Perverse, Horrific Programs, it's not. It originally stood for Personal Home Pages but is now used as the name (Hypertext Preprocessor) for a web scripting language used to produce dynamic web pages.

Unlike HTML, the PHP web programming language is not interpreted by a web browser to produce what's seen. Rather, PHP runs on a web server to create web pages. That means you have to upload your PHP code to test it.

Now PHP code may seem like a bunch of tech mumbo jumbo, however using some of it's pre-defined commands allows you to add some dynamic effects, like the current date, to you web pages with just a few lines of code .

Yes, you can display the current date using other methods like javascript:

<SCRIPT language='JavaScript'type='text/javascript'>
var mydate=new Date()
var year=mydate.getYear()
if (year < 1000)
year+=1900
var day=mydate.getDay()
var month=mydate.getMonth()
var daym=mydate.getDate()
if (daym<10)
daym="0"+daym
var dayarray=new Array("Sunday","Monday","Tuesday","Wednesday",
"Thursday","Friday","Saturday")
var montharray=new Array("January","February","March","April","May","June","July",
"August","September","October","November","December")
document.write("<font color='000000'
face='arial'>"+dayarray[day]+",
"+montharray[month]+" "+daym+",
"+year+"<\/font>")
</SCRIPT>

But with PHP you can let the server get and format the current date using the PHP date command. Doesn't this look a lot easier?

<?php
$today = date("l, F j, Y");
echo "$today";
?>

Both the javascript and the PHP code given above will display the current date in this format:

Day-of-Week, Month Day, Year
like -
Wednesday, July 2, 2008

And if you want to change how the date appears you don't have to change a lot of code. Instead you can just change, add or remove the parameters (the letters inside the quotes after the date command) used for the date command and put spaces and punctuation marks where you want them to appear.

Examples:

date("F j, Y"); - July 2, 2008
date("l, g:i a"); - Wednesday, 9:05 am

Now there are 4 important things you need to know before you use PHP to format the date on you site:

  1. Like I mentioned above, you'll have to upload the code to test it because your PC and web browser most likely can't run PHP code.
  2. The file where you have the PHP code must have the extension of .php. This could be a real negative if you already have your site built in HTML however there is a way around this this rule. (Find out how to use PHP code in a .html file at the end of this article.)
  3. The PHP code must be within the tags. If it is not, the PHP engine on the web server will ignore it.
  4. Each PHP line must end with a semicolon. If it doesn't, you'll get some cryptic error messages.

If you would like to know all the parameters to format the current day in PHP you can find a complete list of the date command parameters at http://www.php.net/date.

As I mentioned in item 2, normally you must use the .php extension for a file to execute PHP code. But since you may already have your site set up in HTML you need to know how to get around this rule. Fortunately I don't have to try to explain how it's done since you can watch 2 short video tutorials. Just open your copy of "29 Easy and Quick Web Design Tricks - Volume 2" and watch videos 13 and 14. (This video ebook is 1 of those included in the "Ultimate Web Videos" package on the Free eBooks, Tools and Videos page in our members area. If you're not a FriendsWhoCare member you can join us here - it's FREE. )

Learning how to format PHP code allows you to make your web pages more dynamic. And with free PHP scripts you can add things like the current date, countdowns, calendars, your site visitor's country or a news tracker.

So don't think of PHP as Perverse, Horrific Programs. Think of PHP as easy way to make your web pages more dynamic!

To Your Success,Susan

If you found this tip useful please let me know by leaving a comment.

Tags: , ,