[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):   February 22, 2017   -- 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 the 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: Wednesday

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): 11:23am   -- 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 © 2017 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	Day of the week, three letters (Mon through Sun)
j	Day of the month without leading zeros (1 to 31)
l 	(lowercase 'L')	A full the day of the week (Sunday)
S	English ordinal suffix for the day of the month, 2 characters
w	Numeric day of the week 0 (Sunday) through 6 (Saturday)
z	The day of the year (0 through 365)

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

	Month	--------------------------------------
F	Full name of a month (January through December)
m	Number of the month, with leading zeros	(01 through 12)
M	Short name of the month, three letters (Jan through Dec)
n	Number of the month, without leading zeros (1 through 12)
t	Number of days in the given month (28 through 31)

	Year	--------------------------------------
L	Leap year (1 if it is a leap year, 0 otherwise).
Y	A full numeric year, 4 digits (Examples: 1999 or 2003)
y	A two digit numeric year (Examples: 99 or 03)

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

	Timezone	--------------------------------------
I 	Daylight saving time (1 if Daylight Saving Time, 0 otherwise)
O	Difference to 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

Please donate $5 below for my development effort -- thank you.

This technique W3C validates under both HTML 4.01 Strict and CSS guidlines.