PHP | DateTime createFromFormat() Function Last Updated : 10 Oct, 2019 Summarize Comments Improve Suggest changes Share Like Article Like Report 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. 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) 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 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) 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) 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 Full Date/Time: U: Seconds since the Unix Epoch (January 1 1970 00:00:00 GMT) 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: php <?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'); ?> Output: 2019-09-30 Program 2: php <?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'); ?> Output: 01-10-2019 11:10:06 Reference: https://www.php.net/manual/en/datetime.createfromformat.php Comment More infoAdvertise with us Next Article PHP | DateTime add() Function K Kanchan_Ray Follow Improve Article Tags : Web Technologies PHP PHP-date-time PHP-function Similar Reads PHP | date_create_from_format() Function The date_create_from_format() is an inbuilt function in php which is used to parses a time string according to a specified format. This function accepts three parameters and returns new DateTime in success or false on failure. Syntax: Procedural style date_create_from_format ( $format, $time, $timez 3 min read PHP | DateTime add() Function The DateTime::add() function is an inbuilt function in PHP which is used to add an amount of time (days, months, years, hours, minutes and seconds) to the given DateTime object. Syntax: Object oriented style: DateTime DateTime::add( DateInterval $interval ) Procedural style: DateTime date_add( DateT 2 min read DateTimeImmutable createFromMutable() Function The DateTimeImmutable::createFromMutable() function is an inbuilt function in PHP which is used to return the new DateTimeImmutable object encapsulating the given DateTime object. Syntax: DateTimeImmutable DateTimeImmutable::createFromMutable( DateTime $object ) Parameters: This function accepts a s 1 min read PHP | DateTime format() Function The DateTime::format() function is an inbuilt function in PHP which is used to return the new formatted date according to the specified format. Syntax: Object oriented style string DateTime::format( string $format ) or string DateTimeImmutable::format( string $format ) or string DateTimeInterface::f 1 min read PHP | date_create(), date_format(), date_add() Functions There is some point in time when we need to add a number of days, months, years, hours, minutes and seconds to Date and time. PHP serves us with several built-in functions to do this. Some built-in functions that we will discuss here are date_create(), date_format() and date_add(). date_create() Fun 3 min read PHP | DateTime sub() Function The DateTime::sub() function is an inbuilt function in PHP which is used to subtract a number of days, months, years, hours, minutes and seconds from a created DateTime object. Syntax: Object oriented style: DateTime DateTime::sub( DateInterval interval ) Procedural style: DateTime date_sub( DateTim 2 min read Like