date_create_from_format


php apg

PARSES a time string according to a specified format.



<?php

/* - - - - - - - - - - - - - - - - -

    Object oriented style
   
   - - - - - - - - - - - - - - - - - */

DateTime|false public static DateTime::createFromFormat str $format 
                                                                                             
str $datetime 
                                                                   
DateTimeZone|null $timezone null )


where,

$format Literal characters
                
SEE the below TABLE )
          
$datetime STRING representing the time

$timezone 
DateTimeZone object representing the desired time zone

?>

<?php

/* - - - - - - - - - - - - - - - - -

      Procedural style
   
   - - - - - - - - - - - - - - - - - */

DateTime|false date_create_from_format str $format 
                                                                  
str $datetime 
                                        
DateTimeZone|null $timezone null )

where,

$format Literal characters
                
SEE the below TABLE )
          
$datetime STRING representing the time

$timezone 
DateTimeZone object representing the desired time zone

?>

 $format 

format character Description Example parsable values
Day
d and j Day of the month,
2 digits with or without leading zeros
01 to 31 or 1 to 31
D and l A textual representation of a day Mon through Sun or Sunday through Sat or Saturday
S English ordinal suffix for the day of the month,
2 characters.
It's ignored while processing.
st, nd, rd or th.
z The day of the year (starting from 0) 0 through 365
Month
F and M A textual representation of a month, such as January or Sept January through December or Jan through Dec
m and n Numeric representation of a month, with or without leading zeros 01 through 12 or 1 through 12
Year
Y A full numeric representation of a year, 4 digits Examples: 1999 or 2003
y A 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 A Ante meridiem and Post meridiem am or pm
g and h 12-hour format of an hour with or without leading zero 1 through 12 or 01 through 12
G and H 24-hour format of an hour with or without leading zeros 0 through 23 or 00 through 23
i Minutes with leading zeros 00 to 59
s Seconds, with leading zeros 00 through 59
u Microseconds (up to six digits) Example: 45, 654321
TimeZone
e, O, P and T TimeZone identifier, or difference to UTC in hours, or difference to UTC with colon between hours and minutes, or TimeZone abbreviation Examples: UTC, GMT, Atlantic/Azores or +0200 or +02:00 or EST, MDT
Full Date/Time
U Seconds since the Unix Epoch
(January 1 1970 00:00:00 GMT)
Example: 1292177455
Whitespace 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: * 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 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.
ed48

The $format must be part of a STRING.
In most cases, the same letters as for the  date  can be used.

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 which begins in 1970-01-01 00:00:00 UTC.


 $datetime 


STRING representing the time.



 $timezone 


If $timezone is omitted and time contains no TimeZone, the current TimeZone will be used.

The $timezone parameter and the current TimeZone are ignored when the time parameter either contains a UNIX timestamp or specifies a TimeZone.





This function returns a new DateTime instance on success or FALSE on failure.

This function is only available in PHP 5.3.0 or higher.


See also: date_create_immutable and date_create_immutable_from_format.



  1 EXERCISE   

<?php

if (version_compare(PHP_VERSION'5.3.0') >= 0)
{

$date01 date_create_from_format('j-M-Y G:i:s''08-Mar-1945 08:33:33');
echo 
date_format($date01'Y-m-d G:i:s') . '<br><br>';

$date02 DateTime::createFromFormat('j-M-Y G:i:s''08-Mar-1945 08:33:33');
echo 
$date02->format('Y-M-d G:i:s') . '<br><br>';

}
else
{
echo 
'FUNÇÃO NÃO SUPORTADA PELA PHP VERSÃO: ' PHP_VERSION '<br><br>';
}

?> 

  2 EXERCISE   

<?php

$date03 
date_create_from_format('j-M-Y H:i:s''09-Feb-2009 15:15:15');
echo 
date_format($date03'Y-m-d G:i:s') . '<br><br>';


$date04 DateTime::createFromFormat('j-M-Y H:i:s''09-Feb-2009 15:15:15');
echo 
$date04->format('Y-M-d G:i:s');

?>