Testing date() PHP Function - Formatting Date and Time

Date and time are used in every Web applications. But formatting a date and time value into a string presentation requires very programming skills. This page allows you to learn and test the date() function that gives so many ways to format a data and time value.

What are date and time formats?

In almost all programming languages, a date and time is stored as large integer number in memory. But when you show a date and time to the user interface, you must convert it into a user-friendly format. Here are some example of date and time formats:

Saturday, 10th of March, 2001, 17:16:17
Sat, 10 Mar 2001 17:16:17 +0200
March 10, 2001, 5:16 pm
10-Mar-2001
3/10/2001
20010310
17:16:17
2001-03-10T17:16:17+0200

The question is how can you convert a date and time value into one of those formats? The answer is that there is always some built-in funtions that you can use to help you. For example, you can use the built-in date() function in PHP to convert a date and time into strings using various formatting codes.

How to use and test date() PHP function?

date() is the most commonly used PHP function to convert a date and time into strings of different formats. date() has a syntax of date(format, timestamp) and returns a string of the specified date and time based on the specified format codes. Example of format codes are:

Y - Year in 4 digits like 1999 or 2003
m - Month in 2 digits like 01 or 12
M - Month in 3 letters like Jan or Dec 
F - Month in full words like January or December
d - Day of the month in 2 digits like 01 or 31
D - Day of the week in 3 letters like Mon or Sun 
l - Day of the week in full words like Monday or Sunday
h - Hour in 12-hour format like 01 or 12 
H - Hour in 24-hour format like 00 or 23 
i - Minute in 2 digits like 00 or 59
s - Second in 2 digits like 00 or 59
T - Timezone like EST or MDT
O - Difference to Greenwich time like +0200 

To help you learn and test date() function, FYIcenter.com has designed this online testing page. All you need to do is to enter the format string in the form below and click the Start button. The converted string will be presented in the result area.

Format:

Popup Windows Tutorials

20 tutorials on different ways to create and test popup windows created in JavaScript: Page unload popups, Pop re-open on close, ...

200+ PHP Script Tutorials

Each tutorial answers one commonly asked PHP programming question with a short, but precise and clear PHP script.