g
Printer Friendly Version

editor  
BellaOnline's PHP Editor
 

The PHP Date Function

The PHP date() function is one of the most useful built-in functions. There are many uses for the PHP built-in date() function. You can use it to simply collect and display the current date and time on you webpages. On the other hand, you can use the date() function as a timestamp for numerous purposes in your PHP programs.

Basically what the date() function does is to return the current date and time using a set of parameters that you control. Click here for a chart that summarizes the controls you will use to format the date() function. Let's take a look at the basic syntax for the PHP data() function.

Basic PHP Code
date(format, [timestamp]);

Example
echo date("g:ia l - F d, Y");


As you can see, the function begins with the function name, date. Inside the parenthesis are the "format controls" and the [optional timestamp] separated by a comma. The whole thing ends with a semicolon.

Optional Timestamp
You might want to supply a special timestamp such as a past date. If a timestamp is not supplied, the current date and time of the server is used by default.

g:ia l - F d, Y
Format
The format is the parameters that you can set to control the way the current date and time is formatted and displayed. In our example, we have formatted the time of day to be displayed as the following when the webpage is viewed on the start of New Years Day 2005.

12:00am Saturday - January 1, 2005

hours : minutes am/pm (12:00am)
day of the week in text (Saturday) syntax is lowercase "L"
hyphen
the month in text (January)
day of the month in digits (1)
comma
the year in digits (2005)

All of this will be displayed in the web browser using the echo statement. You will notice that I have added the punctuation to the string that I want printed to the web browser. I have added a space hyphen space ( - ) between the day of the week and the month. I have also added a comma after January 1. Finally, I have added the colon between hour and minutes. (Remember to escape any special characters that you might add to your datetime string.) When using an echo statement your string must be between quotation marks ("string to be printed to web brower").

The PHP Date Function Format Chart




This site needs an editor - click to learn more!

PHP Site @ BellaOnline
View This Article in Regular Layout

Content copyright © 2013 by Diane Cipollo. All rights reserved.
This content was written by Diane Cipollo. If you wish to use this content in any manner, you need written permission. Contact Editor Wanted for details.



| About BellaOnline | Privacy Policy | Advertising | Become an Editor |
Website copyright © 2023 Minerva WebWorks LLC. All rights reserved.


BellaOnline Editor