DateTime
PHP Manual

DateTime::createFromFormat

(PHP 5 >= 5.3.0)

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

Description

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

Returns new DateTime object formatted according to the specified format.

Parameters

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.

time

String representing the time.

timezone

Time zone.

Return Values

Returns new DateTime instance.

Examples

Example #1 Using ! to reset default date/time values

<?php
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


DateTime
PHP Manual