[web-tips > date-time]

Date and Time

So, you want to place time and/or date in your web site. Well... that's very easy to do and there are several different methods. I prefer to use PHP.

To use php, you need three things:

  • 1. Have php installed on your server (most servers have it installed);
  • 2. Use the following code in your html.
  • 3. Change the sufix on your html to php, such as instead of using "index.html" use "index.php".

Fortunately, you can test this by simply placing the following code in your ".html" document and changing its suffix to a ".php" document.

							
<?php echo date('F j, Y'); ?>

If where you placed the code produces a date, such as (todays date):   May 9, 2008   -- then your server has php installed; and you can use php to display time and date (read on).

The date( ) function has many different settings, which cause the date and time to be formated in various ways. The echo portion of ther statement simply displays the result.

If you change the code to:

							
<?php echo date('l'); ?>

Then the result displayed would be today's day of the week, namely: Friday

Or, if you change the code to:

							
<?php echo date('g:ia'); ?>

The result displayed would be the current time (when this page was created just now): 8:39am   -- get the idea? Also, note the ":" used in the statement and the time displayed.

What would you use this for in your site? Well... I used to find it a chore to always have to redo my web site's copyright every Jan 1 of each year. However, by using the PHP date function, it automatically changes. Try this:

							
Copyright © <?php echo (date("Y")); ?>  Sperling Corporation

The result displayed is: Copyright © 2008 Sperling Corporation     <-- isn't that cool?.

Now, what are all those different settings to make your data() function work, you might ask? Here they are:

	
format character	Description
	Day	--------------------------------------
d	Day of the month, 2 digits with leading zeros	01 to 31
D	A textual representation of a day, three letters	Mon through Sun
j	Day of the month without leading zeros	1 to 31
l 	(lowercase 'L')	A full textual representation of the day of the week
S	English ordinal suffix for the day of the month, 2 characters
w	Numeric representation of the day of the week	0 (Sunday) through 6 (Saturday)
z	The day of the year (starting from 0)	0 through 365

	Week	--------------------------------------
W	ISO-8601 week number of year, weeks starting on Monday. 

	Month	--------------------------------------
F	A full textual representation of a month, January through December
m	Numeric representation of a month, with leading zeros	01 through 12
M	A short textual representation of a month, three letters	Jan through Dec
n	Numeric representation of a month, without leading zeros	1 through 12
t	Number of days in the given month	28 through 31

	Year	--------------------------------------
L	Whether it's a leap year	1 if it is a leap year, 0 otherwise.
Y	A full numeric representation of a year, 4 digits	Examples: 1999 or 2003
y	A two digit representation of a year	Examples: 99 or 03

	Time	--------------------------------------
a	Lowercase Ante meridiem and Post meridiem	am or pm
A	Uppercase Ante meridiem and Post meridiem	AM or PM
B	Swatch Internet time	000 through 999
g	12-hour format of an hour without leading zeros	1 through 12
G	24-hour format of an hour without leading zeros	0 through 23
h	12-hour format of an hour with leading zeros	01 through 12
H	24-hour format of an hour with leading zeros	00 through 23
i	Minutes with leading zeros	00 to 59
s	Seconds, with leading zeros	00 through 59

	Timezone	--------------------------------------
I 	Date is in daylight saving time	1 if Daylight Saving Time, 0 otherwise.
O	Difference to Greenwich time (GMT) in hours	Example: +0200
T	Timezone setting of this machine	Examples: EST, MDT ...
Z	Timezone offset in seconds.

	Full Date/Time	--------------------------------------
r	RFC 2822 formatted date	Example: Thu, 21 Dec 2000 16:01:07 +0200
U	Seconds since the Unix Epoch (January 1 1970 00:00:00 GMT)

Full Reference

This technique validates under both HTML 4.01 and CSS.

Enjoy.

tedd...