Friday, October 17, 2025
HomeLanguagesPHP | date_create_from_format() Function

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, $timezone )

Object oriented style

DateTime::createFromFormat ( $format, $time, $timezone )

Parameters: This function accepts 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: Day of the month, 2 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’s 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 is used as string representing the time.
  • $timezone: This parameter is used as DateTimeZone object representing the desired time zone.

Return Value: This function returns a new DateTime instance on success or FALSE on failure.

Below programs illustrate the date_create_from_format() function in PHP.

Program 1:




<?php
  
// Declare a date in given format
$date = date_create_from_format('D-M-Y', 'monday-Feb-2018');
  
// Output date in given format
echo date_format($date, 'y-n-j');
?>


Output:

18-2-5

Program 2:




<?php
  
// Declare a date in given format
$date = DateTime::createFromFormat('D-M-Y', 'monday-Feb-2018');
  
// Output date in given format
echo $date->format('Y-m-d');
?>


Output:

2018-02-05

Reference: http://php.net/manual/en/datetime.createfromformat.php

RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS