PHP DateTime createFromFormat () function

PHP

Syntax:
  • Object Oriented Style:
    DateTime DateTime::createFromFormat (string  $format, string $time, DateTimeZone $timezone)
  • Procedural style:
    DateTime  date_create_from_format (string $format, string $time, DateTimeZone $timezone)
Parameters:This function takes three parameters as above and described below:
  • $format:these are required parameters that are used to specifying the date format. The next line of parameters is used in the format.
  • Day:
    • d and j:it describes the day of the month. It contains two digits, with or without leading zeros.
    • D and l:textual representation of the day.
    • S:English ordinal suffix for the day of the month, 2 characters. This is ignored during processing.
    • z:day of the year (starting at 0)
  • Month:
    • F and M:textual representation of the month, such as January or September
    • m and n:numeric representation of the month with or without leading zeros
  • Year :
    • Y:full numeric representation of the year, 4 digits
    • y:two-digit year (assuming 1970-2069 inclusive)
  • Time:
    • A and A:ante meridium and post meridium
    • g and h:12- hour format with or without zeros
    • G and H:24-hour hour format with or without leading zeros
    • i: minutes with leading zeros
    • s:seconds, with leading zeros
    • u:microseconds (up to six digits)
  • Time zone:
    • e, O, P and T:time zone identifier, or UTC difference in hours, or the difference from UTC with a colon between hours and minutes, or a time zone abbreviation
  • Full date / time:
    • U:seconds since Unix start (Jan 1, 1970 00:00:00 GMT)
  • Spaces and separators :
    • (space):one space or one tab
    • #:One of the following separator characters :; ,:, / ,. ,,, -, (or)
    • ;,:, / ,. ,,, -, (or):the specified character.
    • ?:Random byte
    • *:Random bytes until next delimiter or number
    • !:Resets all fields (year, month, day, hour, minute, second, fraction and time zone information) to Unix epoch
    • |:Resets all fields (year, month, day, hour, minute, second, fraction, and timezone information) to Unix epoch if not already parsed
    • +:If this format specifier is present, the final data in the line will not throw an error, but instead generate a warning
  • $time:this parameter contains a string that represents the time.
  • $timezone:this parameter contains a DateTimeZone object that represents the desired time zone.
Return Value:this function returns a new DateTime object on success, or F alse on error.The following programs illustrate the DateTime::createFromFormat() function in PHP:Program 1:

 
// Calling the DateTime function: createFromFormat()
// with the format ' jMY ' and the given DateTime $datetime = DateTime::createFromFormat ( 'jMY' , ' 30-September-2019' );  
// Get a new formatted date and time echo $datetime -> format ( 'Ymd' ); ?>
Exit:
2019-09 -30
Program 2:  
// Calling the DateTime function: createFromFormat()
// with the format ' jMY ' and the given DateTime $datetime = DateTime::createFromFormat ( 'jMY' , ' 1-oct-2019' );  
// Get a new formatted date and time echo $datetime -> format ( 'dmY H: i: s' ); ?>
Exit:
01-10 -2019 11:10:06
Link: https: / /www.php.net/manual/en/datetime.createfromformat.php



Get Solution for free from DataCamp guru