Printing DateTimes

suggest change

PHP 4+ supplies a method, format that converts a DateTime object into a string with a desired format. According to PHP Manual, this is the object oriented function:

public string DateTime::format ( string $format )

The function date() takes one parameters - a format, which is a string

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