How to Convert DateTime to String in PHP

Ralfh Bryan Perez Feb 02, 2024
  1. Use format() Method in DateTime Class
  2. Use date_format() Method
  3. Use Predefined Formats in date_d.php
  4. Use list() to Convert DateTime to String in PHP
How to Convert DateTime to String in PHP

Converting DateTime to string in PHP is quite easy, there are useful functions available that are meant for this purpose like format() of DateTime class, date_format(), using the predefined formats or using list().

Use format() Method in DateTime Class

PHP has a class DateTime for managing date and time. It’s been available since PHP version 5.2.

Some reasons why use DateTime over traditional PHP functions to manage dates:

  • It’s able to process more string formats
  • It’s easier than other functions.
  • The interface encapsulates back-end logic with a clean interface to use.

One of the useful methods of DateTime class is the format(), it returns formatted date string according to the given format.

Example:

$theDate    = new DateTime('2020-03-08');
echo $stringDate = $theDate->format('Y-m-d H:i:s');

//output: 2020-03-08 00:00:00

The $stringDate is a string with the provided format.

Note
The DateTime->format() doesn’t really support microseconds.

Use date_format() Method

The date_format() method will return a new DateTime object and format it according to the given format.

Example:

$date = date_create_from_format('d M, Y', '08 Mar, 2020');
echo $newFormat = date_format($date,"Y/m/d H:i:s");

//output: 2020/03/08 00:00:00

According to the provided format, the date_create_from_format() is being used to parses a time string. This function accepts three parameters: format, time, and the timezone (optional).

In the example above, the date_format() processes the created date to convert it to a string.

Use Predefined Formats in date_d.php

List of predefined formats:

define ('DATE_ATOM', "Y-m-d\TH:i:sP");
define ('DATE_COOKIE', "l, d-M-y H:i:s T");
define ('DATE_ISO8601', "Y-m-d\TH:i:sO");
define ('DATE_RFC822', "D, d M y H:i:s O");
define ('DATE_RFC850', "l, d-M-y H:i:s T");
define ('DATE_RFC1036', "D, d M y H:i:s O");
define ('DATE_RFC1123', "D, d M Y H:i:s O");
define ('DATE_RFC2822', "D, d M Y H:i:s O");
define ('DATE_RFC3339', "Y-m-d\TH:i:sP");
define ('DATE_RSS', "D, d M Y H:i:s O");
define ('DATE_W3C', "Y-m-d\TH:i:sP");

Example usage:

$dateFormat = new DateTime(); // this will return current date
echo $stringDate = $date->format(DATE_ATOM);

//output: 2020-03-08T12:54:56+01:00

The example above uses one of the predefined formats, so no need to actually provide it.

Use list() to Convert DateTime to String in PHP

The list() function can be used to assign values to a list of variables in one operation but only works with numerical arrays. When the array is assigned to multiple values, the first item in the array will be assigned to the first variable, and so on, till the end of the number of variables. Though the number of variables cannot be more than the length of the numerical array.

Example:

$date = explode("/",date('d/m/Y/h/i/s')
list($day,$month,$year,$hour,$min,$sec) = $date);
echo $month.'/'.$day.'/'.$year.' '.$hour.':'.$min.':'.$sec;

//output: 03/08/2020 02:01:06

Using the list() function, you can easily use $day, $month, $year, $hour, $min, and $sec as a variable.

Related Article - PHP Date

Related Article - PHP DateTime

Related Article - PHP String