HomeMenu
Jesus · Bible · HTML · CSS · JS · PHP · SVG · Applications

date_create_immutable_from_format

Description

The date_create_immutable_from_format of Date / Time for PHP parses a time string according to a specified format.

Syntax

date_create_immutable_from_format ( string $format , string $datetime [, DateTimeZone $timezone ] ) : DateTimeImmutable

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.

CharacterDescriptionExample
Day
d and jDay of the month, 2 digits with or without leading zeros01 to 31 or 1 to 31
D and lA textual representation of a dayMon through Sun or Sunday through Saturday
SEnglish ordinal suffix for the day of the month, 2 characters. It's ignored while processing.st, nd, rd or th.
zThe day of the year (starting from 0)0 through 365
Month
F and MA textual representation of a month, such as January or SeptJanuary through December or Jan through Dec
m and nNumeric representation of a month, with or without leading zeros01 through 12 or 1 through 12
Year
YA full numeric representation of a year, 4 digitsExamples: 1999 or 2003
yA two digit representation of a year (which is assumed to be in the range 1970-2069, inclusive)Examples: 99 or 03 (which will be interpreted as 1999 and 2003, respectively)
Time
a and AAnte meridiem and Post meridiemam or pm
g and h12-hour format of an hour with or without leading zero1 through 12 or 01 through 12
G and H24-hour format of an hour with or without leading zeros0 through 23 or 00 through 23
iMinutes with leading zeros00 to 59
sSeconds, with leading zeros00 through 59
vMilliseconds (up to three digits)Example: 12, 345
uMicroseconds (up to six digits)Example: 45, 654321
Timezone
e, O, P and TTimezone identifier, or difference to UTC in hours, or difference to UTC with colon between hours and minutes, or timezone abbreviationExamples: UTC, GMT, Atlantic/Azores or +0200 or +02:00 or EST, MDT
Full Date/Time
USeconds since the Unix Epoch (January 1 1970 00:00:00 GMT)Example: 1292177455
Whitespace and Separators
(space)One space or one tabExample:
#One of the following separation symbol: ;, :, /, ., ,, -, ( or )Example: /
;, :, /, ., ,, -, ( or )The specified character.Example: -
?A random byteExample: ^ (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 digitExample: * in Y-*-d with the string 2009-aWord-08 will match aWord
!Resets all fields (year, month, day, hour, minute, second, fraction and timezone information) to the Unix EpochWithout !, 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 yetY-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 insteadUse 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 format does not contain the character ! then portions of the generated time which are not specified in format will be set to the current system time. If format contains the character !, then portions of the generated time not provided in format, 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 timezone is omitted and time contains no timezone, the current timezone will be used. Note: The timezone parameter and the current timezone are ignored when the time parameter either contains a UNIX timestamp (e.g. 946684800) or specifies a timezone (e.g. 2010-01-28T15:00:00+02:00).

Return

Returns a new DateTimeImmutable instance or FALSE on failure.

Examples

1 · format datetime

<?

$format = "j-M-Y";
$datetime = "01-Jan-2001";

$return = date_create_immutable_from_format($format, $datetime);

echo date_format($return, "Y-m-d h:i:s");
2001-01-01 10:28:16

2 · timezone

<?

$format = "j-M-Y";
$datetime = "01-Jan-2001";
$timezone = timezone_open("America/Los_Angeles");

$return = date_create_immutable_from_format($format, $datetime, $timezone);

echo date_format($return, "Y-m-d h:i:s");
2001-01-01 02:28:16