Open In App

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:



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


Article Tags :