Home > Backend Development > PHP Problem > How to convert date to string in php

How to convert date to string in php

王林
Release: 2023-03-02 09:36:01
Original
7116 people have browsed it

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.

How to convert date to string in php

#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);
Copy after login

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)
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template