Home >Backend Development >PHP Problem >How to calculate the difference in days in php

How to calculate the difference in days in php

藏色散人
藏色散人Original
2021-02-23 09:50:594223browse

php method to calculate the difference in days: first create a PHP sample file; then define two time points; then use strtotime to format the time into a timestamp; finally, directly output "$days" to obtain The difference in days.

How to calculate the difference in days in php

#The operating environment of this article: Windows7 system, PHP7.1, Dell G3 computer.

php calculates the number of days difference between two dates, processing time interval

Example: Want to get the number of days difference between 2016-04-10 and 2016-06-15.

$begin_date = strtotime('2016-04-10');
$end_date = strtotime('2016-06-15');
$days = round(($end_date - $begin_date) / 3600 / 24);

Strtotime has been used to format the time into a timestamp, and $days is output directly to obtain the number of days difference.

Attached is also a method for handling time intervals. (Recommended: "PHP Video Tutorial")

/*
 * 处理时间问题
 */
public function get_time($from,$to){
    if($to > $from){
        $miao = $to - $from;
    }else{
        $miao = $from - $to;
    }
    $hour = floor($miao/3600);
    $minute = floor(($miao-$hour*3600)/60);
    $second = $miao - $hour * 3600 - $minute * 60 ;
    $str = '';
    if($hour > 0){
        $str .= $hour.'时';
    }
    if($minute > 0){
        $str .= $minute.'分';
    }
    if($second){
        $str .= $second.'秒';
    }
    return $str;
}

The above is the detailed content of How to calculate the difference in days 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