Open In App

How to convert DateTime to String using PHP ?

Last Updated : 20 Aug, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

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:

PHP
<?php

// Store datetime in variable today
$today = date("d/m/Y H:i:s");    

if ($today) { 
    echo $today; 

// If unknown time 
} else {  
    echo "can't display time"; 
}
?>

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

// Using list method to display the datetime
list($day, $month, $year, $hour, $min, $sec) 
        = explode("/", date('d/m/Y/h/i/s')); 

// Display the datetime
echo $month . '/' . $day . '/' . $year . ' '
    . $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
<?php
// Create a DateTime object
$date = new DateTime('2021-07-05 17:57:38');

// Convert DateTime to string using date_format()
$dateString = date_format($date, 'd/m/Y H:i:s');
echo $dateString;
// Output: 05/07/2021 17:57:38

// Another format example
$dateString2 = date_format($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
<?php
// Create a DateTime object
$date = new DateTime('2021-07-05 17:57:38');

// Format the DateTime object to string using date() and getTimestamp() method
$formattedDate = date('d/m/Y H: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
<?php
// Create a DateTime object
$date = new DateTime("2021-05-07 17:57:38");

// Format the DateTime object to a string using strftime
$dateString = strftime("%d/%m/%Y %H:%M:%S", $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
<?php
$date = new DateTime('2021-07-05 17:57:38', new DateTimeZone('America/New_York'));
$formatter = new IntlDateFormatter(
    'en_US', // Locale
    IntlDateFormatter::FULL, 
    IntlDateFormatter::FULL, 
    'America/New_York', 
    IntlDateFormatter::GREGORIAN, 
    'dd/MM/yyyy HH:mm:ss' 
);
$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
<?php
// Create a DateTimeImmutable object
$date = new DateTimeImmutable('2021-07-05 17:57:38');

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

Output
05/07/2021 17:57:38


Next Article

Similar Reads