Home  >  Article  >  Backend Development  >  How to convert date to string in php?

How to convert date to string in php?

coldplay.xixi
coldplay.xixiOriginal
2020-07-10 14:23:433938browse

PHP date to string method: first find the difference between the two dates; then use JS to get the current date, and convert the normal date format into a timestamp. The code is [$year=(( int)substr("2008-12-04",0,4));】.

How to convert date to string in php?

php method to convert date to string:

1. Find the difference between two dates

For example, the date difference between 2007-3-5 ~ 2007-3-6

echo abs(strtotime("2007-3-5") - strtotime("2007-3-6"))/60/60/24;
echo "天<br>";

2. JS to get the current date

var myDate = new Date();
myDate.getYear();        //获取当前年份(2位)
myDate.getFullYear();    //获取完整的年份(4位,1970-????)
myDate.getMonth();       //获取当前月份(0-11,0代表1月)
myDate.getDate();        //获取当前日(1-31)
myDate.getDay();         //获取当前星期X(0-6,0代表星期天)
myDate.getTime();        //获取当前时间(从1970.1.1开始的毫秒数)
myDate.getHours();       //获取当前小时数(0-23)
myDate.getMinutes();     //获取当前分钟数(0-59)
myDate.getSeconds();     //获取当前秒数(0-59)
myDate.getMilliseconds();    //获取当前毫秒数(0-999)
myDate.toLocaleDateString();     //获取当前日期
var mytime=myDate.toLocaleTimeString();     //获取当前时间
myDate.toLocaleString( );        //获取日期与时间

3. PHP date and timestamp conversion to each other

There are two types of PHP time: one is timestamp type(1228348800), the other isNormal date format(2008-12-4)

So there are two forms of saving to the database. I save the timestamp type as a string, which is more convenient.

Normal date type is saved as DATE type.

Please pay attention to these two. I usually use two types, and save the time type as DATE. I still keep Saved with timestamp, the data could not be written into the table. After debugging for a long time, I found out the error. The type was inconsistent and the data was not written into the library.

Convert 1228348800 to 2008-12-4 format code as follows:

$date3=date(&#39;Y-m-d H:i:s&#39;,"1228348800");

This is OK. If you still want to get hours, minutes and seconds, just change 'Y-m-d' , but please note that the PHP time still seems to have an error of 8 hours. If you add it, it will be OK.

The timestamp can be converted to a normal date. On the contrary, how can the normal date format be converted to a timestamp? , please look at the following code:

$year=((int)substr("2008-12-04",0,4));//取得年份
$month=((int)substr("2008-12-04",5,2));//取得月份
$day=((int)substr("2008-12-04",8,2));//取得几号
echo mktime(0,0,0,$month,$day,$year);

Related learning recommendations: PHP programming from entry to proficiency

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!

Statement:
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