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

How to convert date type in php

王林
王林Original
2021-09-01 15:59:221850browse

The method for date type conversion in php is: use the date() function to convert a UNIX timestamp to a date; use the strtotime() function to convert a date to a UNIX timestamp.

How to convert date type in php

The operating environment of this article: windows10 system, php 7.1, thinkpad t480 computer.

Date format conversion can be completed in PHP, but one disadvantage is that it takes up the parsing time of the PHP parser, so the speed will be relatively slow. But this method also has the advantage that it can be converted regardless of whether the data obtained by query in the database is not limited to the conversion range.

Next let’s take a look at how to implement it.

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

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

Specific code implementation:

$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/>";

PHP Chinese website 17th online class (PHP training) is now open! If you also love PHP programming, come and learn with me!

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