How to convert DateTime to String using PHP ? (original) (raw)

Last Updated : 20 Aug, 2024

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

Table of Content

**1. By using the Format Method

**Example:

PHP `

`

**Output:

07/05/2021 17:57:38

**2. By using list() Method

month,month, month,year, hour,hour, hour,min, $sec) = explode("/", date('d/m/Y/h/i/s')); // Display the datetime echo month.′/′.month . '/' . month./.day . '/' . $year . ' ' . hour.′:′.hour . ':' . hour.:.min . ':' . $sec; ?>

`

**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.

PHP `

dateString=dateformat(dateString = date_format(dateString=dateformat(date, 'd/m/Y H:i:s'); echo $dateString; // Output: 05/07/2021 17:57:38 // Another format example dateString2=dateformat(dateString2 = date_format(dateString2=dateformat(date, 'Y-m-d H:i:s'); echo "\n" . $dateString2; // Output: 2021-07-05 17:57:38 ?>

`

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.

PHP `

formattedDate=date(′d/m/YH:i:s′,formattedDate = date('d/m/Y H:i:s', formattedDate=date(d/m/YH:i:s,date->getTimestamp()); // Output the result echo $formattedDate; ?>

`

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.

PHP `

dateString=strftime("dateString = strftime("%d/%m/%Y %H:%M:%S", dateString=strftime("date->getTimestamp()); echo $dateString; ?>

`

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.

PHP `

dateString=dateString = dateString=formatter->format($date); echo $dateString; ?>

`

**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.

PHP `

dateString=dateString = dateString=date->format('d/m/Y H:i:s'); echo $dateString; // Contributed by Nikunj Sonigara ?>

`

Output

05/07/2021 17:57:38