Open In App

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:



Parameters: This function uses three parameters as mentioned above and described below:

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
  
// 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
  
// 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


Article Tags :