search
HomeBackend DevelopmentPHP ProblemHow to calculate the difference in days in php

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment