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:


DateTime class:
* Printing DateTimes

Table Of Contents
2 Arrays
4 Types
10 Cookies
14 JSON
15 SOAP
17 cURL
19 XML
21 Traits
35 UTF-8
36 URLs
38 PHPDoc
41 Loops
44 Closur
58 DateTime class
72 YAML
77 Cache
78 Streams
81 PDO
82 SQLite3
83 Sockets
87 MongoDB
93 IMAP
94 Redis
95 Imagick
102 APCu
108 PSR