PHP Date function tutorial gives you examples and a table of date format
PHP Date function tutorial gives you examples and a table of date formatPHP Date Function
In this section we will show you the different formatting options available in the PHP Date function
PHP date() function provides many formatting options. You can use the options to format the date output in the required format.
The general format is: string date ( string $date [, int timestamp] )
Following tables shows formats and its description in the PHP.
FORMAT |
DESCRIPTION |
VALUE RETURNED |
|
DATE | d | Day of the month, 2 digits with leading zeros | 01 through31 |
j | Day of the month but with no leading zeros | 1 to 31 | |
D | A textual representation of day | Mon through Sun | |
l | Full textual representation of the day | Monday to Sunday | |
w | Numeric representation of the day | 0 = Sunday,......,6 = Saturday | |
z | The day of the year | 0 through 365 | |
WEEK | W | ISO-8601 week number of year | 38 (The 38th week of year) |
MONTH | m | Numeric representation of month, with leading zeros | 01 through 12 |
n | Numeric representation of month, without leading zeros | 1 through 12 | |
F | Textual representation of month | January through December | |
M | Short Textual representation of month | Jan through Dec | |
t | No. of days of given month | 28 through 31 | |
YEAR | Y | Full numeric representation of year | 1988, 2000, 2009 etc. |
y | Short numeric representation of year | 99, 09 etc. | |
TIME | a | Lowercase am or pm | am or pm |
A | Uppercase am or pm | AM or PM | |
g | 12-hour format of an hour without leading zeros | 1 through 12 | |
G | 24-hour format of an hour without leading zeros | 0 through 23 | |
h | 12-hour format of an hour with leading zeros | 01 through 12 | |
H | 24-hour format of an hour with leading zeros | 00 through 23 | |
TIME ZONE | e | Timezone identifier (included in PHP 5.1.0) | UTC, GMT, Atlantic/Azores |
O | Difference with Greenwich Mean Time in hours | Example: +530 | |
P | Difference with Greenwich Mean Time in hours with a colon (included in PHP 5.1.3) | Example: +5:30
|
Following example code shows how you can use the format options to display the formatted date. Our program will display the dates in two formats:
a) October 9, 2009, Friday,12:34 pm
b) it is tenth month of the year
PHP Date formatting example code:
<?php
echo ("<B>Different format of Date is given below </b><br>");
echo ("<br>");
echo ("<br>");
/*If we want to get date format as October 9, 2009, Friday, 5:55 pm</b>*/
echo ("<br>");
echo date("F j, Y, l,g:i a");
echo ("<br>");
/*We can print any message using date function as follows:</b>*/
echo ("<br>");
echo date ('\i\t \i\s \t\e\n\t\h \m\o\n\t\h \o\f \t\h\e \y\e\a\r');
echo ("<br>");
?>
Output:
Different format of Date is given below
October 9, 2009, Friday,12:34 pm
it is tenth month of the year
With the help of above example and table given above we can get any kind of required date format.