Printing DateTimes

suggest change

DateTime::format, added in PHP 5.2, converts a DateTime object into a string. The manual describes it as:

public string DateTime::format ( string $format )

date() takes a format string and an optional Unix timestamp. The format letters are the same ones DateTime::format uses.

Format

The format is a string, and uses single characters to define the format:

Usage

These characters can be used in various combinations to display times in virtually any format. Here are some examples:

$date = new DateTime('2000-05-26T13:30:20'); /* Friday, May 26, 2000 at 1:30:20 PM */

$date->format("H:i");
/* Returns 13:30 */

$date->format("H i s");
/* Returns 13 30 20 */

$date->format("h:i:s A");
/* Returns 01:30:20 PM */

$date->format("j/m/Y");
/* Returns 26/05/2000 */

$date->format("D, M j 'y - h:i A");
/* Returns Fri, May 26 '00 - 01:30 PM */

Procedural

The procedural format is similar:

Object-Oriented

$date->format($format);

Procedural Equivalent

date_format($date, $format);

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents