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

How to convert time format in php

王林
王林Original
2020-11-03 17:14:482092browse

How to convert time format in php: You can use date function to convert time format, such as [$y=date("Y",time()); $m=date("m",time() ); $d=date("d",time());】.

How to convert time format in php

The method is as follows:

(Recommended tutorial: php video tutorial)

1. Complete the conversion in MySQL

in the 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()

How to convert time format in php

2. Complete the conversion in PHP

Complete the conversion in the PHP program. The advantage is that it can be converted regardless of whether the data is obtained by querying in the database. The conversion range is not limited. The disadvantage is that it takes up PHP parsing. The parsing time of the processor is relatively slow

1. Use the function to convert UNIX timestamp to date: date()

2. Use the 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/>";

Related recommendations: php training

The above is the detailed content of How to convert time 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