Home > Article > Backend Development > How to convert time into string in php
php method to convert time into a string: 1. Use the format method of the DateTime class to convert; 2. Convert through the date_format method; 3. Use the predefined format in date_d.php to convert; 4 , use the list function for conversion.
Recommended: "PHP Video Tutorial"
Convert DateTime to String in PHP
Use the format() method of the DateTime class
Use the date_format() method
Use the predefined format in date_d.php
Use the list() function to convert the date to a string
In PHP Converting DateTime to string is very easy, there are some dedicated functions. For example, use a predefined format or use list().
Use the format() method of the DateTime class
The PHP DateTime class is used to manage dates and times. It was introduced in PHP version 5.2.
Some advantages of using DateTime over traditional PHP functions to manage dates:
It can handle many more string formats
easily than other functions.
This interface uses backend logic with a clean interface.
One of the useful methods of the DateTime class is format(), which returns a 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
$stringDate is now used to provide the formatted string.
Note: DateTime-> format() doesn't really support microseconds.
Using the date_format() method
The date_format() method will return a new DateTime object, and 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
date_create_from_format() is used to parse a time string according to the provided format. This function accepts three parameters, namely format, time and timezone (optional).
In the above example, 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");
Usage example:
$dateFormat = new DateTime(); // this will return current date echo $stringDate = $date->format(DATE_ATOM); //output: 2020-03-08T12:54:56+01:00
The above example uses one The format is predefined so there is no need to actually provide it.
Use the list() function to convert a date to a string
The list() function can be used to assign values to a list of variables in one operation, but only for numeric arrays. When assigning an array to multiple values, the first item in the array is assigned to the first variable, and so on until the end of the number of variables. However, the number of variables cannot exceed the length of the numeric 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 variables.
The above is the detailed content of How to convert time into string in php. For more information, please follow other related articles on the PHP Chinese website!