PHP date_format() Function

In this chapter you will learn:

  1. Definition for PHP date_format() Function
  2. Syntax for PHP date_format() Function
  3. Parameter for PHP date_format() Function
  4. Format for PHP date_format() Function
  5. Return value from PHP date_format() Function
  6. Example - Return a new DateTime object, and then format the date

Definition

The date_format() function formats a date for the specified format.

Syntax

PHP date_format() Function has the following syntax.

date_format(object,format);

Parameter

ParameterIs RequiredDescription
objectRequired.DateTime object returned by date_create()
formatRequired.Format for the date

Format

Format character Description Example
aLowercase am/pm am or pm
AUppercase am/pm AM or PM
B Swatch Internet Time 000 to 999
c ISO 8601 date, time, and time zone 2004-06-18T09:26:55+01:00
d 2-digit day of month, leading zeros 01 to 31
D Day string, three letters Mon, Thu, Sat
F Month string, fullJanuary, August
g 12-hour clock hour, no leading zeros 1 to 12
G24-hour clock hour, no leading zeros 0 to 23
h 12-hour clock hour, leading zeros01 to 12
H 24-hour clock hour, leading zeros00 to 23
i Minutes with leading zeros 00 to 59
I Is daylight savings time active? 1 if yes, 0 if no
jDay of month, no leading zeros 1 to 31
l Day string, full Monday, Saturday
L Is it a leap year?1 if yes, 0 if no
m Numeric month, leading zeros01 to 12
M Short month stringJan, Aug
n Numeric month, no leading zeros 1 to 12
O Difference from GMT 200
r RFC-822 formatted date Sat, 22 Dec 1979 17:30 +0000
sSeconds, with leading zeros 00 to 59
SEnglish ordinal suffix for day number st, nd, rd, or th
tNumber of days in month28 to 31
TTime zone for server GMT, CET, EST
U Unix Timestamp 1056150334
wNumeric day of week 0 (Sunday), 6 (Saturday)
WISO-8601 week number of year30 (30th week of the year)
yTwo-digit representation of year 97, 02
YFour-digit representation of year1997, 2002
zDay of year 0 to 366
ZTime zone offset in seconds -43200 to 43200
DATE_ATOM Atom 2013-04-12T15:52:01+00:00
DATE_COOKIE HTTP Cookies Friday, 12-Apr-13 15:52:01 UTC
DATE_ISO8601 ISO-8601 2013-04-12T15:52:01+0000
DATE_RFC822 RFC 822 Fri, 12 Apr 13 15:52:01 +0000
DATE_RFC850 RFC 850Friday, 12-Apr-13 15:52:01 UTC
DATE_RFC1036 RFC 1036Fri, 12 Apr 13 15:52:01 +0000
DATE_RFC1123 RFC 1123Fri, 12 Apr 2013 15:52:01 +0000
DATE_RFC2822 RFC 2822 Fri, 12 Apr 2013 15:52:01 +0000
DATE_RFC3339 Same as DATE_ATOM (since PHP 5.1.3)2013-04-12T15:52:01+00:00
DATE_RSS RSSFri, 12 Aug 2013 15:52:01 +0000
DATE_W3C World Wide Web Consortium 2013-04-12T15:52:01+00:00

Return

PHP date_format() Function returns the formatted date as a string. FALSE on failure.

Example

Return a new DateTime object, and then format the date:


<?php//ja va 2s .c om
$date=date_create("2013-03-15");
echo date_format($date,"Y/m/d H:i:s");
echo "\n";
$date=date_create("2013-03-15");
echo date_format($date,DATE_ATOM);
?>

The code above generates the following result.

Next chapter...

What you will learn in the next chapter:

  1. Definition for PHP date_get_last_errors() Function
  2. Syntax for PHP date_get_last_errors() Function
  3. Return value from PHP date_get_last_errors() Function
  4. Example - return the warnings and errors while parsing a date string
Home » PHP Tutorial » PHP Date Functions
PHP checkdate() Function
PHP date() Function
PHP date_add() Function
PHP date_create() Function
PHP date_create_from_format() Function
PHP date_date_set() Function
PHP date_default_timezone_get() Function
PHP date_default_timezone_set() Function
PHP date_diff() Function
PHP date_format() Function
PHP date_get_last_errors() Function
PHP date_interval_format() Function
PHP date_isodate_set() Function
PHP date_modify() Function
PHP date_offset_get() Function
PHP date_parse() Function
PHP date_parse_from_format() Function
PHP date_sub() Function
PHP date_sun_info() Function
PHP date_sunrise() Function
PHP date_sunset() Function
PHP date_time_set() Function
PHP date_timestamp_get() Function
PHP date_timestamp_set() Function
PHP date_timezone_get() Function
PHP date_timezone_set() Function
PHP getdate() Function
PHP gettimeofday() Function
PHP gmdate() Function
PHP gmmktime() Function
PHP gmstrftime() Function
PHP idate() Function
PHP localtime() Function
PHP microtime() Function
PHP mktime() Function
PHP strftime() Function
PHP strptime() Function
PHP strtotime() Function
PHP time() Function
PHP Timezones Constant
PHP timezone_abbreviations_list() Function
PHP timezone_identifiers_list() Function
PHP timezone_location_get() Function
PHP timezone_name_from_abbr() Function
PHP timezone_name_get() Function
PHP timezone_offset_get() Function
PHP timezone_open() Function
PHP timezone_version_get() Function