How to convert time type in php

coldplay.xixi
Release: 2023-03-04 20:00:02
Original
2860 people have browsed it

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’);
Copy after login

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

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

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

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

You can also directly convert using the native php class

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

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));
Copy after login

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);
}
Copy after login

$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!

Related labels:
php
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template