Display date using date function in PHP
This is a short tutorial on the date function in PHP. Its aimed at newcomers to the PHP language and shows some simple yet effective ways of displaying the date in your PHP pages. The tutorial also gives examples of arguments given to the date function.
Although many users have the time set to display in their taskbar, some web designers want to include it into their page. This tutorial will show you how to do just that using PHP and the date() function. We will also explain some of the formatting used for displaying the date in your way. First we’ll explain what the date() function is and how to call it.
The date function is just like any other function in PHP. You would call it just like you would time(), etc. The following would display the date in the format ‘month . day . Year’.
<?
$current_date = date(”m.d.Y”);
echo $current_date;
?>
As always the <? and ?> are just the opening and closing PHP tags. All PHP scripts must start with the opening and closing tags. First we start off with the variable $current_date. What’s happening here is we’re using the assignment operator ‘=’ to assign a value to $current_date. The value is going to be the result of the function date(). date() is a function call, and will return the current system date. The stuff on the inside of the parenthesis is arguments we send to the function. If we wanted to change the way the date is displayed, we would change to argument passed to the date function. date(”Y”) would only return the current year. Following is the arguments that date() will accept:
a - “am” or “pm”
A - “AM” or “PM”
d - day of the month, 2 digits with leading zeros; i.e. “01″ to “31″
D - day of the week, textual, 3 letters; i.e. “Fri”
F - month, textual, long; i.e. “January”
h - hour, 12-hour format; i.e. “01″ to “12″
H - hour, 24-hour format; i.e. “00″ to “23″
g - hour, 12-hour format without leading zeros; i.e. “1″ to “12″
G - hour, 24-hour format without leading zeros; i.e. “0″ to “23″
i - minutes; i.e. “00″ to “59″
j - day of the month without leading zeros; i.e. “1″ to “31″
l (lowercase ‘L’) - day of the week, textual, long; i.e. “Friday”
L - boolean for whether it is a leap year; i.e. “0″ or “1″
m - month; i.e. “01″ to “12″
n - month without leading zeros; i.e. “1″ to “12″
M - month, textual, 3 letters; i.e. “Jan”
s - seconds; i.e. “00″ to “59″
S - English ordinal suffix, textual, 2 characters; i.e. “th”, “nd”
t - number of days in the given month; i.e. “28″ to “31″
U - seconds since the epoch
w - day of the week, numeric, i.e. “0″ (Sunday) to “6″ (Saturday)
Y - year, 4 digits; i.e. “1999″
y - year, 2 digits; i.e. “99″
z - day of the year; i.e. “0″ to “365″
Z - time zone offset in seconds (i.e. “-43200″ to “43200″)
As you can see there are many ways you can display the date. How you want to is totally up to you. Now we’ll get back to the example. Once the date function is called we terminate the line using a semicolon ‘;’ Now we have our variable $current_date we can write it to a database, display it, etc. In this example we are just going to display it. We make it display by using ‘echo’ Using echo prints the variable $current_date which, you guessed it, contains the results of our date function. This line is also terminated using the semicolon ‘;’. The results of our script would be something like this:
11.21.2005
It doesn’t seem like much, but can be used in so many ways. A more practical example follows:
<?
$current_date = date(”a”);
if($current_date == “am”) {
echo “Good Morning”;
} elseif ($current_date == “pm”) {
echo “Good Evening”;
}
?>
That is just a simple script that will greet the user depending on if its am or pm and display the appropriate greeting. For more information on PHP and the date function, you can visit the PHP website and PrimeGraphix website.
This tutorial was written for PrimeGraphix Web Design and Development. If you would like to reprint this article, please contact us for licensing details. This tutorial is copyright 2005 by PrimeGraphix.com.
Published in: PHP