Home > Article > Backend Development > How to convert date format in php
How to convert date format in php: 1. Use the "date()" function to convert the UNIX timestamp into a date function; 2. Use the "strtotime()" function to convert the date into a UNIX timestamp function.
php date format conversion
1. Complete in MySQL
Recommendation:《PHP Tutorial》
Convert in MySQL query statement. The advantage is that it does not take up the parsing time of the PHP parser and is fast. The disadvantage is that it can only be used in database queries and has limitations.
1. Use the function to convert UNIX timestamp to date: FROM_UNIXTIME()
2. Use the function to convert date to UNIX timestamp: UNIX_TIMESTAMP()
eg:
2. Complete the conversion in PHP
Complete the conversion in the PHP program. The advantage is that no matter whether the data obtained by querying in the database is It can be converted, and the conversion range is not limited. The disadvantage is that it takes up the parsing time of the PHP parser and is relatively slow.
1. Use the function to convert UNIX timestamp to date: date()
2. Use function to convert date to UNIX timestamp: strtotime()
$y=date("Y",time()); //年 $m=date("m",time()); //月 $d=date("d",time()); //日 echo $y."<br/>"; echo $m."<br/>"; echo $d."<br/>"; $eight_clock = mktime(8, 0, 0, $m, $d ,$y); //每天8点 echo date("Y-m-d H:i:s",$eight_clock)."<br/>"; $day_time = mktime(0, 0, 0, $m, 1 ,$y); //每月1号 echo date("Y-m-d H:i:s",$day_time)."<br/>";
The above is the detailed content of How to convert date format in php. For more information, please follow other related articles on the PHP Chinese website!