VOOZH about

URL: https://www.geeksforgeeks.org/php/how-to-convert-datetime-to-string-using-php/

⇱ How to convert DateTime to String using PHP ? - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

How to convert DateTime to String using PHP ?

Last Updated : 23 Jul, 2025

Converting a `DateTime` to a string in PHP involves formatting the `DateTime` object into a human-readable format using the `format()` method. This process allows you to represent dates and times as strings in various formats, such as Y-m-d H:i:s.  

There are some following approaches

1. By using the Format Method

  • In the Format method, we can convert the DateTime object to a string.
  • The DateTime to string program is programmed below:

Example:

Output:

07/05/2021 17:57:38

2. By using list() Method

  • The list() method is used to convert the DateTime object to string.
  • The shorter way of list method is programmed below:

Output:

05/07/2021 05:58:48

Using date_format() Function

The date_format() function provides an alternative way to format a DateTime object into a string. It is functionally similar to the format method of the DateTime class.

Example: In this example, the date_format() function takes a DateTime object and a format string as parameters. It formats the DateTime object into a string according to the specified format and returns the result.


Output
05/07/2021 17:57:38
2021-07-05 17:57:38

Using date() and getTimestamp() Method

The date() function in PHP can be used to format a timestamp into a human-readable string. By using the getTimestamp() method of the DateTime object, you can get the Unix timestamp and then format it with the date() function.

Example: This method offers an alternative way to format a DateTime object to a string, utilizing the date() function and the Unix timestamp provided by the getTimestamp() method.


Output
05/07/2021 17:57:38

Using strftime() Function

The strftime() function in PHP formats a local time/date according to locale settings. This function is particularly useful for producing date and time strings in a locale-aware manner.

Example: The following example demonstrates how to use the strftime() function to convert a DateTime object to a string.


Output
07/05/2021 17:57:38

Using IntlDateFormatter Class

The IntlDateFormatter class provides a way to format dates and times according to locale-specific patterns. This can be particularly useful for internationalization.

Example: This example demonstrates how to use the IntlDateFormatter class to convert a DateTime object to a string.

Output

05/07/2021 17:57:38

Using DateTimeImmutable Class

The DateTimeImmutable class works similarly to the DateTime class but ensures that the date and time values cannot be modified after the object is created. This can be useful when you want to ensure the immutability of your date and time objects. You can still format the DateTimeImmutable object into a string using the format() method.

Example: The following example demonstrates how to use the DateTimeImmutable class to convert a date and time to a string.


Output
05/07/2021 17:57:38
Comment