Syntax: - Object Oriented Style:
DateTimeImmutable static DateTime::createFromFormat (string $format, string $time, DateTimeZone $timezone)
- Procedural style:
DateTimeImmutable date_create_from_format (string $format, string $time, DateTimeZone $timezone)
Parameters:This function takes three parameters as above and described below:
- $format:This parameter contains the DateTime format in string.
- $time:this parameter contains the time in string format.
- $timezone:this parameter contains a DateTimeZone object.
Return Value:This function returns a new DateTimeImmutable object representing the date and time specified in a time string that has been formatted in the specified format.
Per characters and their description: Format character Desciption Example returned values < / tr> j Day of the month without leading zeros 1 to 31 d Day of the month with leading zeros 01 to 31 m Numeric representation of the month 01 to 12 M Short textual representation of the month Jan to Dec Y Representation of a year 1989, 2017 < / tbody> The format string can be a combination of any format characters in any order, but we would have to provide the input date and time string in the same order.The programs below illustrate the date_create_immutable_from_format function() in PHP:
Program 1:
// Use the date_create_from_format() function
// create date format
$date
= date_create_from_format (
’jMY’
,
’ 03-oct-2019’
);
// Show date
echo
date_format (
$date
,
’Ymd’
);
?>
Exit:2019-10 -03
Program 2:
// Use the date_create_from_format() function
// create date format
$date
= date_create_from_format (
’ dMY’
,
’03-oct-2019’
);
// Show date
echo
date_format (
$date
,
’Ymj’
);
?>
Exit:2019-10 -3
Program 3:
// Use the DateTime::createFromFormat() function
// create a date object
$date
= DateTime::createFromFormat ( ’jMY’
,
’ 03-oct-2019’
);
// Show date
echo
$date
-> format (
’ Ymd’
);
?>
Exit:2019-10 -03
Link: https://www.php. net / manual / en / datetimeimmutable.createfromformat.php