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::format( string $format )
- Procedural style
string date_format( DateTimeInterface $object, string $format )
Parameters: This function uses two parameters as mentioned above and described below:
- $object: This parameter holds the DateTime object.
- $format: This parameter holds the format accepted by date() function.
Return Value: This function return the new formatted date string on success or False on failure.
Below programs illustrate the DateTime::format() function in PHP:
Program 1:
<?php
$datetime = new DateTime( '2019-09-30' );
echo $datetime ->format( 'd-m-Y' );
?>
|
Program 2:
<?php
$datetime = new DateTime( '2019-09-30' );
echo $datetime ->format( 'd-m-Y H:i:s' );
?>
|
Output:
30-09-2019 00:00:00
Reference: https://www.php.net/manual/en/datetime.format.php
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!