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:
php
php
- Object oriented style
string DateTime::format( string $format )
orstring DateTimeImmutable::format( string $format )
orstring DateTimeInterface::format( string $format )
- Procedural style
string date_format( DateTimeInterface $object, string $format )
- $object: This parameter holds the DateTime object.
- $format: This parameter holds the format accepted by date() function.
<?php
// Initialising the DateTime() object with a date
$datetime = new DateTime('2019-09-30');
// Calling the format() function with a
// specified format 'd-m-Y'
echo $datetime->format('d-m-Y');
?>
Output:
Program 2:
30-09-2019
<?php
// Initialising the DateTime() object with a date
$datetime = new DateTime('2019-09-30');
// Calling the format() function with a
// specified format 'd-m-Y H:i:s'
echo $datetime->format('d-m-Y H:i:s');
?>
Output:
Reference: https://www.php.net/manual/en/datetime.format.php30-09-2019 00:00:00