date_create_immutable_from_format


php apg

PARSES a time STRING according to a specified format.



<?php

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

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

DateTimeImmutable|false 
DateTimeImmutable
::createFromFormat str $format 
                                                                 
str $datetime 
                                        
DateTimeZone|null $timezone null )


where,

$format The format STRING accepted by the function date 

$datetime 
A date/time STRING according the Date and Time Formats  

$timezone 
A DateTimeZone object representing the TIMEZONE 

?>

<?php

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

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

DateTimeImmutable|false
date_create_immutable_from_format 
str $format 
                                                            
str $datetime 
                                   
DateTimeZone|null $timezone null 


where,

$format The format STRING accepted by the function date 

$datetime 
A date/time STRING according the Date and Time Formats  

$timezone 
A DateTimeZone object representing the TIMEZONE

?>

 $format 

RECOGNIZED CHARACTERS
format character Description Example returned values
Day
d Day of the month, 2 digits with leading zeros 01 to 31
D A textual representation of a day, three letters Mon through Sun
j Day of the month without leading zeros 1 to 31
l A full textual representation of the day of the week Sunday through Saturday
N ISO-8601 numeric representation of the day of the week
Since PHP 5.1.0
1 (for Monday) through 7 (for Sunday)
S English ordinal suffix for the day of the month, 2 characters st, nd, rd or th.
Works well with j
w Numeric representation of the day of the week 0 (for Sunday) through 6 (for Saturday)
z The day of the year (starting from 0) 0 through 365
Week
W ISO-8601 week number of year, weeks starting on Monday Example:
42 (the 42nd week in the year)
Month
F A full textual representation of a month, such as January or March January through December
m Numeric representation of a month, with leading zeros 01 through 12
M A short textual representation of a month, three letters Jan through Dec
n Numeric representation of a month, without leading zeros 1 through 12
t Number of days in the given month 28 through 31
Year
L Whether it's a leap year 1 if it is a leap year, 0 otherwise.
o ISO-8601 week-numbering year.
This has the same value as Y,
except that if the ISO week number (W) belongs to the previous or next year, that year is used instead.
Since PHP 5.1.0
Examples:
1999 or 2018
Y A full numeric representation of a year, 4 digits Examples:
1999 or 2018
y A two digit representation of a year Examples:
99 or 18
Time
a Lowercase Ante meridiem and Post meridiem am or pm
A Uppercase Ante meridiem and Post meridiem AM or PM
B Swatch Internet time 000 through 999
g 12-hour format of an hour without leading zeros 1 through 12
G 24-hour format of an hour without leading zeros 0 through 23
h 12-hour format of an hour with leading zeros 01 through 12
H 24-hour format of an hour with leading zeros 00 through 23
i Minutes with leading zeros 00 to 59
s Seconds, with leading zeros 00 through 59
u Microseconds - Since PHP 5.2.2.
Note that date will always generate 000000
since it takes an integer parameter,
whereas DateTime::format does
support microseconds if DateTime created with microseconds.
Example: 654321
v Milliseconds - Since PHP 7.0.0.
Same note applies as for u.
Example: 654
Timezone
e Timezone identifier
Since PHP 5.1.0
Examples: UTC, GMT, Atlantic/Azores
I (capital i) Whether or not the date is in Daylight Saving Time 1 if DST, 0 if not DST.
O Difference to Greenwich time (GMT) in hours Example: +0200
P Difference to Greenwich time (GMT)
with colon between hours and minutes.
Since PHP 5.1.3
Example: +02:00
T Timezone abbreviation Examples: EST, MDT ...
Z Timezone offset in seconds.
The offset for timezones west of UTC
is always negative,
and for those east of UTC is always positive.
-43200 through 50400
Full Date/Time
c ISO 8601 date (added in PHP 5) 2004-02-12T15:19:21+00:00
r RFC 2822 formatted date Example:
Thu, 21 Dec 2000 16:01:07 +0200
U Seconds since the Unix Epoch:
January 1 1970 00:00:00 GMT
See also the function time
ed48


 $time 


STRING representing the date/time according the Date and Time Formats.



 $timezone 


A DateTimeZone object representing the TimeZone.





This function works like DateTime::createFromFormat  but works with DateTimeImmutable class.



  1 EXERCISE   

<?php

$tzobj_01 
= new DateTimeZone("America/New_York");

$dat_01 date_create_immutable_from_format'Y-F-d H:i:s''now'$tzobj_01 );
$objfr_01 = new DateTime();

// echo 'date_create_immutable_from_format(<br> \'Y-F-d H:i:s\', <br>\'now\', <br>$tzobj_01 )<br><br>';

echo $objfr_01->format('Y-F-d H:i:s') . '<br><br>';

echo 
$objfr_01->getOffset() . '<br><br>';

echo 
$objfr_01->getTimestamp() . '<br><br>';

$obtz_01 $objfr_01->getTimezone();

foreach(
$obtz_01 as $vl01 => $v01 )
{
     echo 
$vl01 ' = ' $v01 '<br>';
}

?> 

  2 EXERCISE   

<?php

$tdate021 
DateTimeImmutable::createFromFormat('d-m-Y H:i:s''10-08-2012 10:10:10');
// format string 'd-m-Y H:i:s' is COMPATIBLE with the date '10-08-2012 10:10:10

echo '$tdate021 = DateTimeImmutable::createFromFormat(\'d-m-Y H:i:s\', \'10-08-2012 10:10:10\')';

echo 
'<br>$tdate021->format(\'F d, Y H:i:s\')<br>';

echo 
$tdate021->format('F d, Y H:i:s');

echo 
'<br><br><br>';

$tdate022 DateTimeImmutable::createFromFormat('d-M-Y H:i:s''10-08-2012 10:10:10');
// format string 'd-M-Y H:i:s' is INCOMPATIBLE with the date '10-08-2012 10:10:10

echo '$tdate022 = DateTimeImmutable::createFromFormat(\'d-M-Y H:i:s\', \'10-08-2012 10:10:10\')';

echo 
'<br>$tdate022->format(\'F d, Y H:i:s\')<br><br>&nbsp;&nbsp;* * * E R R 0 R * * *<br><br>';

// echo $tdate022->format('F d, Y H:i:s');

?>  

  3 EXERCISE   

<?php

$tzobj_03 
= new DateTimeZone("America/New_York");

$objfr_03 = new DateTimeImmutable('2005-03-08 08:33:33');

$dat_03 date_create_immutable_from_format'Y-F-d H:i:s''2005-03-08 08:33:33'$tzobj_03 );

print_r($objfr_03);

echo 
'<br><br>' date_format($objfr_03'Y/m/d H:i:s') . '<br><br>';

echo 
$objfr_03->format('Y-F-d H:i:s') . '<br><br>';

?> 

  4 EXERCISE   

<?php

date_default_timezone_set
('America/New_York');

$en_strf04 'Y-m-d G:i:s'

$ts_now04 time();

$datef_04 =  date($en_strf04$ts_now04); 

$date04 date_create_immutable_from_format($en_strf04$datef_04);

echo 
date_format($date04$en_strf04) . '<br><br>';

?> 

  5 EXERCISE   

<?php 

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


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

?> 

  6 EXERCISE   

<?php 

date_default_timezone_set
('Europe/Lisbon');

$objtz06 = new DateTimezone('Europe/Lisbon');

$en_strf06 'Y-m-d G:i:s';  

$ts_now06 time(); 

$datef_06 =  date($en_strf06$ts_now06);  

$date06a 
date_create_immutable_from_format($en_strf06$datef_06$objtz06); 
echo 
date_format($date06a'Y-m-d G:i:s') . '<br><br>'


$date06b 
DateTimeImmutable::createFromFormat($en_strf06$datef_06$objtz06); 
echo 
$date06b->format('Y-M-d G:i:s'); 

?> 

  7 EXERCISE   

<?php

$date07a 
= new DateTime("now", new DateTimeZone("America/Sao_Paulo"));

echo 
$date07a->format(DateTime::W3C) . '<br><br>';

$date07b 
DateTimeImmutable::createFromFormat(DateTime::ATOM
                                 
$date07a->format(DateTime::ATOM));

echo 
$date07b->format(DateTime::COOKIE);

?>