Monday 2 February 2015

Date Format in PHP for date month year and time

Date Format in PHP for date month year and time

We can apply date format on date objects returned by or created from DateTime function or any other way . Here are some example of displaying date and time by using format.

date_default_timezone_set('America/Chicago');
$date = new DateTime('2012-04-15 22:15:40');
echo $date->format('Y/m/d H:i:s');

The output is here

2012/04/15 22:15:40

Examples:
echo date('t'); //will return number of days in current month
echo date('Y'); //will return Year in four digits: 2013

Characters used for formating string in PHP date format

a'am' or 'pm'
A'AM' or 'PM'
BSwatch Internet time
dday of the month, 2 digits with leading zeros; i.e. '01' to '31'
Dday of the week, textual, 3 letters; i.e. 'Fri'
Fmonth, textual, long; i.e. 'January'
ghour, 12-hour format without leading zeros; i.e. '1' to '12'
Ghour, 24-hour format without leading zeros; i.e. '0' to '23'
hhour, 12-hour format; i.e. '01' to '12'
Hhour, 24-hour format; i.e. '00' to '23'
iminutes; i.e. '00' to '59'
I (capital i)'1' if Daylight Savings Time, '0' otherwise.
jday of the month without leading zeros; i.e. '1' to '31'
l (lowercase 'L')day of the week, textual, long; i.e. 'Friday'
Lboolean for whether it is a leap year; i.e. '0' or '1'
mmonth; i.e. '01' to '12'
Mmonth, textual, 3 letters; i.e. 'Jan'
nmonth without leading zeros; i.e. '1' to '12'
rRFC 822 formatted date; i.e. 'Thu, 21 Dec 2000 16:01:07 +0200' (added in PHP 4.0.4)
sseconds; i.e. '00' to '59'
SEnglish ordinal suffix, textual, 2 characters; i.e. 'th', 'nd'
tnumber of days in the given month; i.e. '28' to '31'
TTimezone setting of this machine; i.e. 'MDT'
Useconds since the epoch
wday of the week, numeric, i.e. '0' (Sunday) to '6' (Saturday)
Yyear, 4 digits; i.e. '1999'
yyear, 2 digits; i.e. '99'
zday of the year; i.e. '0' to '365'
Ztimezone offset in seconds (i.e. '-43200' to '43200'). The offset for timezones west of UTC is always negative, and for those east of UTC is always positive.
You can use the above date format for formating php today date and displaying php date and time in different ways.

0 comments:

Post a Comment