Saturday, February 21, 2026
HomeLanguagesPHP | DateTime format() Function

PHP | DateTime format() Function

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

30-09-2019

Program 2:




<?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:

30-09-2019 00:00:00

Reference: https://www.php.net/manual/en/datetime.format.php

RELATED ARTICLES

Most Popular

Dominic
32506 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6882 POSTS0 COMMENTS
Nicole Veronica
12005 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12099 POSTS0 COMMENTS
Shaida Kate Naidoo
7011 POSTS0 COMMENTS
Ted Musemwa
7255 POSTS0 COMMENTS
Thapelo Manthata
6967 POSTS0 COMMENTS
Umr Jansen
6956 POSTS0 COMMENTS