The way PHP converts dates into strings is: we can do it through the date() function. The date() function is used to format the local date and time. If successful, it will return the formatted date string. If it fails, it will report an E_WARNING error and return FALSE.
#To convert a date timestamp into a time string, you can use the date() function.
(Recommended tutorial: php tutorial)
Function introduction:
date() function is used to format local date and time and return the format ized date string.
Syntax
date(format,timestamp);
Returns the string generated by converting the integer timestamp according to the given format string. If no timestamp is given, the local current time is used. In other words, timestamp is optional and the default value is time().
Return value:
If successful, the formatted date string will be returned. If failed, an E_WARNING error will be reported and FALSE will be returned.
Code implementation:
$time_str = date('Y-m-d H:i:s', time()); // 将时间戳转化为相应的时间字符串 echo $time_str; // string(19) "2018-01-17 02:24:34" //反之 $time_int = strtotime($time_str); // 将时间字符串转化为时间戳 echo $time_int; // int(1516155874)
The above is the detailed content of How to convert date to string in php. For more information, please follow other related articles on the PHP Chinese website!