Home  >  Article  >  Backend Development  >  How to convert date format in php

How to convert date format in php

藏色散人
藏色散人Original
2020-07-22 10:26:292614browse

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.

How to convert date format in php

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:

 How to convert date format in php

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!

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