How to convert DateTime to string in PHP

  1. Using format() method in DateTime class
  2. Using date_format() Method
  3. Using predefined formats in date_d.php
  4. Using list() 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().

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

Using date_format() Method

The date_format() method will return a new DateTime object, then format the date 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

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

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

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

Using 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

  • How to add days to Date in PHP
  • How to calculate the difference between two dates using PHP
  • How to convert string to date and date-time in PHP
  • How to Convert One Date Format to Another in PHP
  • How to Get the Current Date and Time in PHP
  • Related Article - PHP DateTime

  • How to add days to Date in PHP
  • How to calculate the difference between two dates using PHP
  • How to convert string to date and date-time in PHP
  • How to Convert One Date Format to Another in PHP
  • How to Get the Current Date and Time in PHP
  • How to get the last day of the month with PHP functions
  • How to calculate the difference between two dates using PHP
  • How to Get the Current Year in PHP
  • How to Convert a Date to a Timestamp in PHP
  • How to Convert a Timestamp to a Readable Date or Time in PHP
  • Related Article - PHP String

  • How to add days to Date in PHP
  • How to calculate the difference between two dates using PHP
  • How to convert string to date and date-time in PHP
  • How to Convert One Date Format to Another in PHP
  • How to Get the Current Date and Time in PHP
  • How to get the last day of the month with PHP functions
  • How to calculate the difference between two dates using PHP
  • How to Get the Current Year in PHP
  • How to Convert a Date to a Timestamp in PHP
  • How to Convert a Timestamp to a Readable Date or Time in PHP
  • How to convert string to date and date-time in PHP
  • How to Convert an Integer into a String in PHP
  • How to Convert an Array to a String in PHP
  • How to Convert a String to a Number in PHP
  • How to Get the Last Character of a String in PHP
  • How to Check If a String Starts With a Specified String in PHP
  • comments powered by Disqus