PHP

 Home  Computers & Internet  Web Programming PHP
What is PHP?
Echo
Comments
Variables
Constants
Data Types
number_format()
Character Strings
Mathematical Operators
Comparison Operators
Logical Operators
Joining Strings
explode()
implode()
strtolower()
strtoupper()
strlen()
ucfirst()
ucwords()
strrev()
str_replace()
str_repeat()
trim()
strip_tags()
addslashes()
stripslashes()
strpos()
strrpos()
nl2br()
isset()
unset()
empty()
POST
GET
If Statements
If Else Statements
Elseif Statements
Switch Statements
For Loops
While Loops
Do While Loops
Foreach Loops
File Create
File Open
File Read
File Write
File Delete
fgets()
file_get_contents()
Date & Time
$_SERVER
Sessions
Cookies
Arrays

HOW TO FORMAT DATE AND TIME IN PHP WITH THE date() FUNCTION

Date and time are easily formatted to human readable form using PHP's in-built date() function. This function takes a format as a parameter and an optional timestamp. If no timestamp is given, it defaults to the current time() timestamp of the server.

A timestamp, also known as a Unix Timestamp, is the number of seconds since 00:00:00 GMT on 1 January 1970.

The general format of the date function is as follows:

date(date format);

Example 1:

$date=date("d/m/Y");
echo $date;

This will output:

20/11/2008

Example 2:

$date=date("d-m-Y");
echo $date;

This will output:

20-11-2008

Example 3:

$date=date("d-m-Y G:i:s");
echo $date;

This will output:

20-11-2008 17:16:55

The following table lists the common formats that you can use for this function:

Format Example
d 01 to 31
D Mon to Sun
j 1 to 31
l Sunday to Saturday
N 1 for Monday to 7 for Sunday
S st, nd, rd or th
w 0 for Sunday to 6 for Saturday
z 0 to 365
W Example: 25 would be 25th week in the year
F January to December
m 01 to 12
M Jan to Dec
n 1 to 12
t Number of days a month has: 28, 29, 30 or 31
L 1 for leap year, 0 if not
Y Eg: 1978, 2008
y Eg: 78 or 08
a am or pm
A AM or PM
g 12 hour without leading zeros format: 1 to 12
G 24 hour without leading zeros format: 0 to 23
h 12 hour with leading zeros format: 01 to 12
H 24 hour with leading zeros format: 00 to 23
i minutes with leading zeros: 00 to 59
s seconds with leading zeros: 00 to 59




See also:

time()
mktime()
gmdate()
strtotime()
microtime()


Home | Privacy Policy | Terms Of Use | Contact Us