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

How to convert time type in php

coldplay.xixi
coldplay.xixiOriginal
2020-08-20 09:54:482863browse

php method for time type conversion: 1. Use the function [strtotime()] to convert [Y-m-d] to a timestamp, such as [strtotime ('2017-08-22')]; 2. Use the function [date()] converts the timestamp into [Y-m-d H:i:s].

How to convert time type in php

php method for time type conversion:

php time format conversion function is date( ),strtotime() function, PHP’s native time class can also convert time format.

1. Convert Y-m-d to timestamp Example:

2017-08-22 转化为时间戳    
strtotime(‘2017-08-22’);

2. Convert timestamp to Y-m-d H:i:s

date("Y-m-d H:i:s",strtotime('2017-08-22'));

3. Time Ymd format is converted to Y-m-d

date(“Y-m-d”,strtotime("20170822"));

You can also directly convert using the native php class

var_dum(\DateTime::createFromFormat('Ymd','20170822')->format('Y-m-d'));

4. Get the current timestamp: 1. time(); 2. date('U');

5. Tomorrow's time format

date("Y-m-d H:i:s",strtotime(+1 day));

Get the date for a period of time Search the code

$end = new \DateTime($end);
$end = $end->modify( '+1 day' );
$interval = new \DateInterval('P1D');// yii中引用原生的php类加\,因为有命名空间
$daterange = new \DatePeriod($start, $interval ,$end);//查询这个时间段内所有的日期
foreach($daterange as $date){
    $single_date = $date->format("Ymd");//每个日期都改成20170022的格式
    $this->run_curl($url,$post_data,$project,$flow,$single_date,$timeBegin,$timeEnd);
}

$datarange is the date within the time period.

Related learning recommendations: php programming (video)

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