XHanch Studio Log in | Register | Cart

Forum

[How to] Formatting...
 
Notifications
Clear all

[How to] Formatting date and time in PHP

1 Posts
1 Users
0 Likes
1,123 Views
XHanch
(@xhanch-alt)
Posts: 2105
Member Admin
Topic starter
 

Formatting a date time in PHP is pretty easy. you just need to use date function

date(string $format[,int $timestamp]);

Here are available date and time variables:

Day
d – day of the month 2 digits (01-31)
j – day of the month (1-31)
D – 3 letter day (Mon – Sun)
l – full name of day (Monday – Sunday)
N – 1=Monday, 2=Tuesday, etc (1-7)
S – suffix for date (st, nd, rd)
w – 0=Sunday, 1=Monday (0-6)
z – day of the year (1=365)

Week
W – week of the year (1-52)

Month
F – Full name of month (January – December)
m – 2 digit month number (01-12)
n – month number (1-12)
M – 3 letter month (Jan – Dec)
t – Days in the month (28-31)

Year
L – leap year (0 no, 1 yes)
o – ISO-8601 year number (Ex. 1979, 2006)
Y – four digit year (Ex. 1979, 2006)
y – two digit year (Ex. 79, 06)

Time
a – am or pm
A – AM or PM
B – Swatch Internet time (000 – 999)
g – 12 hour (1-12)
G – 24 hour c (0-23)
h – 2 digit 12 hour (01-12)
H – 2 digit 24 hour (00-23)
i – 2 digit minutes (00-59)
s 0 2 digit seconds (00-59)

Other
e – timezone (Ex: GMT, CST)
I – daylight savings (1=yes, 0=no)
O – offset GMT (Ex: 0200)
Z – offset in seconds (-43200 – 43200)
r – full RFC 2822 formatted date

Examples

<?php
    //By    : Xhanch Studio
    //URL  : http://xhanch.com/

    //Prints something like: Monday
    echo date("l");

    //Prints something like: Monday 8th of August 2005 03:12:46 PM
    echo date('l jS of F Y h:i:s A');

    //Prints: July 1, 2000 is on a Saturday
    echo "July 1, 2000 is on a " . date("l", mktime(0, 0, 0, 7, 1, 2000));

    //prints something like: Mon, 15 Aug 2005 15:12:46 UTC
    echo date(DATE_RFC822);

    //prints something like: 2000-07-01T00:00:00+00:00
    echo date(DATE_ATOM, mktime(0, 0, 0, 7, 1, 2000));
?>

Source: http://xhanch.com/php-script-using-mysqli/

 
Posted : 02/03/2011 11:26 pm
Share:

× Close Menu