PHP Date + Time Primer - Retrieving the server's timestamp (Page 2 of 3 )
No matter which operating system you are running your web server and PHP on, the procedure for gathering the current date and time is the same. The first problem is that the returned value is fairly cryptic.
For example, the following PHP code:
<? echo time(); ?>
Will quickly translate into:
Though this may not be very user friendly, it is an accurate measure of the date and time. This numeric date and time format originates from the fact that all Unix servers tell time by counting the number of seconds that have passed since 12:00 a.m. on January 1, 1970. All we need to do is learn how to display the above information in a way that makes sense to our site visitors.
Displaying the date in a user friendly format Using PHP to convert the Unix timestamp into something more understandable is just a matter of employing the proper function. This tutorial will cover the use of a variety of basic functions available for this task.
The most commonly used function is date(). As you might expect, this function is fairly customizable. Customization is achieved through PHP's recognition of a variety of characters submitted in the format string. Below are just a few common characters, for a complete list of them, visit the PHP documentation website.
D: day of the week
F: the month
h: hour, 12-hour format
H: hour, 24-hour format
l: day of the week
If we combine some of this knowledge into PHP code, the following:
<? echo date("l F d Y h:i" ,time()); ?>
should give a result similar to:
Monday June 17 2002 02:32
Obviously this makes considerably more sense than the previous Unix timestamp. The result could be further improved through the addition of punctuation. When additional text is introduced into the format string it is important to remember to use a backslash to comment it out. Otherwise, your text will be recognized as part of the special character string.
Perhaps a more elegant method of accomplishing tasks like this is to use the strftime() function instead. This function works exactly the same as the date() function except that a percent sign (%) is placed before each of the special characters in the format string. There are a few differences between the formatting characters so be sure to check the PHP documentation.
The following characters are different from the previous date() examples:
%A: The day of the week
%B: The month in textual form
Consider the following scenario. You are interested in including the date and/or time in a regular phrase to make the output more appealing to the site visitors.
To do this, you could use the following code:
<? echo strftime("Today is %A, %B %d, %Y." ,time()); ?>
The result is:
Today is Monday, June 17, 2002.
As you can see, in this instance, the strftime() function is slightly less cumbersome than the date() function.
Outputting the date from an array Another method available for displaying the date and time is the getdate() function. This function takes the timestamp argument and creates an array of the date and time elements. Below are the array arguments that the getdate() function will accept.
"mday": day of the month
"wday": numeric day of the week
"mon": month in a numeric format
"yday": day of the year
"weekday": day of the week
"month": month in a textual format
Using these array arguments, it is fairly simple to output the desired date and time elements. For example, this rather clunky PHP Code:
The above example is admittedly not the best application for this function. Using the getdate() function becomes especially useful if you wish to display different aspects of the current date and time on completely different portions of a web page. Perhaps the following example will demonstrate this function's use more effectively.
<html> <head> <title>Sample Getdate() function</title> </head> <body> <? $date_time = getdate (time()); ?> Hello! Welcome to our humble website. It is now <? echo $date_time [ "month" ] ?> of <? echo $date_time [ "year" ] ?>. It's hard to believe so much of this year has passed us by. It seems like only yesterday that </body> </html>
The page would display an output similar to the following:
Hello! Welcome to our humble website. It is now June of 2002. It's hard to believe so much of this year has passed us by. It seems like only yesterday that...