PHP Portal » PHP Handbuch » DateTime::createFromFormat

Werbung

DateTime::createFromFormat


(PHP 5 >= 5.3.0)

DateTime::createFromFormatReturns new DateTime object formatted according to the specified format

Beschreibung

public static DateTime DateTime::createFromFormat ( string $format , string $time [, DateTimeZone $timezone ] )

Returns new DateTime object formatted according to the specified format.

Parameter-Liste

format

Format accepted by date().

If format does not contain the character ! then portions of the date/time value specified in format but not specified in time will be set to the current system time.

If format contains the character !, then portions of the generated time specified to the left-hand side of the ! in format will be set to corresponding values from the Unix epoch.

If the first character of format is !, then all portions of the date/time value generated which are not specified in time will be initialized to corresponding values from the Unix epoch.

The Unix epoch is 1970-01-01 00:00:00 UTC.

time

String representing the time.

timezone

Time zone.

Rückgabewerte

Returns new DateTime instanceIm Fehlerfall wird FALSE zurückgegeben..

Beispiele

PHP Code
1
2
3
4
5
6
7
8
9
10
11
12
13
echo "Current system date and time: " . date('Y-m-d H:i:s') . "\n"; $format = 'Y-m-d'; $dt = DateTime::createFromFormat($format, '2009-02-03'); echo "Format: $format; " . $dt->date . "\n"; $format = 'Y-m-d H:i:s'; $dt = DateTime::createFromFormat($format, '2009-02-03 15:16:17'); echo "Format: $format; " . $dt->date . "\n"; $format = 'Y-m-!d H:i:s'; $dt = DateTime::createFromFormat($format, '2009-02-03 15:16:17'); echo "Format: $format; " . $dt->date . "\n"; $format = '!Y-m-d'; $dt = DateTime::createFromFormat($format, '2009-02-03'); echo "Format: $format; " . $dt->date . "\n";

The above example will output something like the following (taking into account the current system time):

Current system date and time: 2009-09-13 01:04:03
Format: Y-m-d; 2009-02-03 01:04:03
Format: Y-m-d H:i:s; 2009-02-03 15:16:17
Format: Y-m-!d H:i:s; 1970-01-03 15:16:17
Format: !Y-m-d; 2009-02-03 00:00:00

Siehe auch