DateTime::createFromFormat
date_create_from_format
(PHP 5 >= 5.3.0, PHP 7)
DateTime::createFromFormat -- date_create_from_format — Parses a time string according to a specified format
Description
Object oriented style
public static DateTime::createFromFormat ( string $format , string $datetime [, DateTimeZone|null $timezone = null ] ) : DateTime|false
Procedural style
date_create_from_format ( string $format , string $datetime [, DateTimeZone|null $timezone = null ] ) : DateTime|false
Returns a new DateTime object representing the date and time specified by the datetime string, which was formatted in the given format.
Parameters
-
format -
The format that the passed in string should be in. See the formatting options below. In most cases, the same letters as for the date() can be used.
The following characters are recognized in the formatparameter stringformatcharacterDescription Example parsable values Day --- --- dandjDay of the month, 2 digits with or without leading zeros 01to31or1to31DandlA textual representation of a day MonthroughSunorSundaythroughSaturdaySEnglish ordinal suffix for the day of the month, 2 characters. It's ignored while processing. st,nd,rdorth.zThe day of the year (starting from 0) 0through365Month --- --- FandMA textual representation of a month, such as January or Sept JanuarythroughDecemberorJanthroughDecmandnNumeric representation of a month, with or without leading zeros 01through12or1through12Year --- --- YA full numeric representation of a year, 4 digits Examples: 1999or2003yA two digit representation of a year (which is assumed to be in the range 1970-2069, inclusive) Examples: 99or03(which will be interpreted as1999and2003, respectively)Time --- --- aandAAnte meridiem and Post meridiem amorpmgandh12-hour format of an hour with or without leading zero 1through12or01through12GandH24-hour format of an hour with or without leading zeros 0through23or00through23iMinutes with leading zeros 00to59sSeconds, with leading zeros 00through59vMilliseconds (up to three digits) Example: 12,345uMicroseconds (up to six digits) Example: 45,654321Timezone --- --- e,O,PandTTimezone identifier, or difference to UTC in hours, or difference to UTC with colon between hours and minutes, or timezone abbreviation Examples: UTC,GMT,Atlantic/Azoresor+0200or+02:00orEST,MDTFull Date/Time --- --- USeconds since the Unix Epoch (January 1 1970 00:00:00 GMT) Example: 1292177455Whitespace and Separators --- --- (space) One space or one tab Example: #One of the following separation symbol: ;,:,/,.,,,-,(or)Example: /;,:,/,.,,,-,(or)The specified character. Example: -?A random byte Example: ^(Be aware that for UTF-8 characters you might need more than one?. In this case, using*is probably what you want instead)*Random bytes until the next separator or digit Example: *inY-*-dwith the string2009-aWord-08will matchaWord!Resets all fields (year, month, day, hour, minute, second, fraction and timezone information) to the Unix Epoch Without !,all fields will be set to the current date and time.|Resets all fields (year, month, day, hour, minute, second, fraction and timezone information) to the Unix Epoch if they have not been parsed yet Y-m-d|will set the year, month and day to the information found in the string to parse, and sets the hour, minute and second to 0.+If this format specifier is present, trailing data in the string will not cause an error, but a warning instead Use DateTime::getLastErrors() to find out whether trailing data was present. Unrecognized characters in the format string will cause the parsing to fail and an error message is appended to the returned structure. You can query error messages with DateTime::getLastErrors().
To include literal characters in
format, you have to escape them with a backslash (\).If
formatdoes not contain the character!then portions of the generated time which are not specified informatwill be set to the current system time.If
formatcontains the character!, then portions of the generated time not provided informat, as well as values to the left-hand side of the!, will be set to corresponding values from the Unix epoch.The Unix epoch is 1970-01-01 00:00:00 UTC.
-
datetime -
String representing the time.
-
timezone -
A DateTimeZone object representing the desired time zone.
If
timezoneis omitted ornullanddatetimecontains no timezone, the current timezone will be used.Note:
The
timezoneparameter and the current timezone are ignored when thedatetimeparameter either contains a UNIX timestamp (e.g.946684800) or specifies a timezone (e.g.2010-01-28T15:00:00+02:00).
Return Values
Returns a new DateTime instance or false on failure.
Changelog
| Version | Description |
|---|---|
| 7.3.0 | The v format specifier has been added. |
Examples
Example #1 DateTime::createFromFormat() example
Object oriented style
<?php
$date = DateTime::createFromFormat('j-M-Y', '15-Feb-2009');
echo $date->format('Y-m-d');
?> Procedural style
<?php
$date = date_create_from_format('j-M-Y', '15-Feb-2009');
echo date_format($date, 'Y-m-d');
?> The above examples will output:
2009-02-15
Example #2 Intricacies of DateTime::createFromFormat()
<?php
echo 'Current time: ' . date('Y-m-d H:i:s') . "\n";
$format = 'Y-m-d';
$date = DateTime::createFromFormat($format, '2009-02-15');
echo "Format: $format; " . $date->format('Y-m-d H:i:s') . "\n";
$format = 'Y-m-d H:i:s';
$date = DateTime::createFromFormat($format, '2009-02-15 15:16:17');
echo "Format: $format; " . $date->format('Y-m-d H:i:s') . "\n";
$format = 'Y-m-!d H:i:s';
$date = DateTime::createFromFormat($format, '2009-02-15 15:16:17');
echo "Format: $format; " . $date->format('Y-m-d H:i:s') . "\n";
$format = '!d';
$date = DateTime::createFromFormat($format, '15');
echo "Format: $format; " . $date->format('Y-m-d H:i:s') . "\n";
?> The above example will output something similar to:
Current time: 2010-04-23 10:29:35 Format: Y-m-d; 2009-02-15 10:29:35 Format: Y-m-d H:i:s; 2009-02-15 15:16:17 Format: Y-m-!d H:i:s; 1970-01-15 15:16:17 Format: !d; 1970-01-15 00:00:00
Example #3 Format string with literal characters
<?php
echo DateTime::createFromFormat('H\h i\m s\s','23h 15m 03s')->format('H:i:s');
?> The above example will output something similar to:
23:15:03
See Also
- DateTime::__construct() - Returns new DateTime object
- DateTime::getLastErrors() - Returns the warnings and errors
- checkdate() - Validate a Gregorian date
- strptime() - Parse a time/date generated with strftime
© 1997–2020 The PHP Documentation Group
Licensed under the Creative Commons Attribution License v3.0 or later.
https://www.php.net/manual/en/datetime.createfromformat.php