PHP | DateTime createFromFormat() Function

The DateTime::createFromFormat()function is an inbuilt function in PHP which returns a new DateTime object representing the date and time format.

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 uses three parameters as mentioned above and described below:



  • $format: It is a required parameters which is used to specify the date format. The following parameters string are used in format.
    1. Day:
      • d and j: It describes the day of the month. It contains two digits with or without leading zeros.
      • D and l: A textual representation of a day.
      • S: English ordinal suffix for the day of the month, 2 characters. It is ignored while processing.
      • z: The day of the year (starting from 0)
    2. Month:
      • F and M: A textual representation of a month, such as January or Sept
      • m and n: Numeric representation of a month, with or without leading zeros
    3. Year:
      • Y: A full numeric representation of a year, 4 digits
      • y: A two digit representation of a year (which is assumed to be in the range 1970-2069, inclusive)
    4. Time:
      • a and A: Ante meridiem and Post meridiem
      • g and h: 12-hour format of an hour with or without leading zero
      • G and H: 24-hour format of an hour with or without leading zeros
      • i: Minutes with leading zeros
      • s: Seconds, with leading zeros
      • u: Microseconds (up to six digits)
    5. 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
    6. Full Date/Time:
      • U: Seconds since the Unix Epoch (January 1 1970 00:00:00 GMT)
    7. Whitespace and Separators:
      • (space): One space or one tab
      • #: One of the following separation symbol: ;, :, /, .,,, -, ( or )
      • ;, :, /, .,,, -, ( or ): The specified character.
      • ?: A random byte
      • *: Random bytes until the next separator or digit
      • !: Resets all fields (year, month, day, hour, minute, second, fraction and timezone information) to the Unix Epoch
      • |: Resets all fields (year, month, day, hour, minute, second, fraction and timezone information) to the Unix Epoch if they have not been parsed yet
      • +: If this format specifier is present, trailing data in the string will not cause an error, but a warning instead
  • $time: This parameter holds the string which represents the time.
  • $timezone: This parameter holds the DateTimeZone object which represents the desired time zone.

Return Value: This function returns the new DateTime object on success or False on failure.

Below programs illustrate the DateTime::createFromFormat() function in PHP:

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Calling the DateTime:createFromFormat() function
// with the format 'j-M-Y' and given DateTime is 
$datetime = DateTime::createFromFormat('j-M-Y', '30-September-2019');
  
// Getting the new formatted datetime 
echo $datetime->format('Y-m-d');
?>

chevron_right


Output:

2019-09-30

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Calling the DateTime:createFromFormat() function
// with the format 'j-M-Y' and given DateTime is 
$datetime = DateTime::createFromFormat('j-M-Y', '1-oct-2019');
  
// Getting the new formatted datetime 
echo $datetime->format('d-m-Y H:i:s');
?>

chevron_right


Output:

01-10-2019 11:10:06

Reference: https://www.php.net/manual/en/datetime.createfromformat.php



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.