Home php教程 php手册 php 计算两个日期这间的间隔天数

php 计算两个日期这间的间隔天数

May 25, 2016 pm 04:54 PM
explode strtotime

本文章来给各位同学详细介绍关于php 计算两个日期这间的间隔天数实例,各位同学可参考,我们一般是把日期用strtotime转换,然后再进行算,这样可以精确到时分秒去哦。


例1

直接把日期转换

 代码如下 复制代码

function daysbetweendates($date1, $date2){
    $date1 = strtotime($date1);
    $date2 = strtotime($date2);
    $days = ceil(abs($date1 - $date2)/86400);
    return $days;
}

例2

 代码如下 复制代码

functionmaketime($date)
{
list($year,$month,$day) = explode('-',$date);
returnmktime(0,0,0,$month,$day,$year);
}
$date1 = '2007-01-08';
$date2 = '2007-03-01';
$d = (maketime($date2) - maketime($date1)) / (3600*24);
echo'相差$d 天';

?>


例3

PHP实现两个日期间隔的年、月、周、日数的计算

 

 代码如下 复制代码
    function format($a,$b){
        //检查两个日期大小,默认前小后大,如果前大后小则交换位置以保证前小后大
        if(strtotime($a)>strtotime($b)) list($a,$b)=array($b,$a);
        $start  = strtotime($a);
        $stop   = strtotime($b);
        $extend = ($stop-$start)/86400;
        $result['extends'] = $extend;
        if($extend            $result['daily'] = $extend;
        }elseif($extend            if($stop==strtotime($a.'+1 month')){
                $result['monthly'] = 1;
            }else{
                $w = floor($extend/7);
                $d = ($stop-strtotime($a.'+'.$w.' week'))/86400;
                $result['weekly']  = $w;
                $result['daily']   = $d;
            }
        }else{
            $y=    floor($extend/365);
            if($y>=1){                //如果超过一年
                $start = strtotime($a.'+'.$y.'year');
                $a     = date('Y-m-d',$start);
                //判断是否真的已经有了一年了,如果没有的话就开减
                if($start>$stop){
                    $a = date('Y-m-d',strtotime($a.'-1 month'));
                    $m =11;
                    $y--;   
                }
                $extend = ($stop-strtotime($a))/86400;
            }
            if(isset($m)){
                $w = floor($extend/7);
                $d = $extend-$w*7;
            }else{
                $m = isset($m)?$m:round($extend/30);
                $stop>=strtotime($a.'+'.$m.'month')?$m:$m--;
                if($stop>=strtotime($a.'+'.$m.'month')){
                    $d=$w=($stop-strtotime($a.'+'.$m.'month'))/86400;
                    $w = floor($w/7);
                    $d = $d-$w*7;
                }
            }
            $result['yearly']  = $y;
            $result['monthly'] = $m;
            $result['weekly']  = $w;
            $result['daily']   = isset($d)?$d:null;
        }
        return array_filter($result);
    }
 
    print_r(format('2012-10-1','2012-12-15'));
?>



教程地址:

欢迎转载!但请带上文章地址^^

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

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

Repo: How To Revive Teammates
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to use PHP explode function and solve errors How to use PHP explode function and solve errors Mar 10, 2024 am 09:18 AM

The explode function in PHP is a function used to split a string into an array. It is very common and flexible. In the process of using the explode function, we often encounter some errors and problems. This article will introduce the basic usage of the explode function and provide some methods to solve the error reports. 1. Basic usage of the explode function In PHP, the basic syntax of the explode function is as follows: explode(string$separator,string$stri

Common errors and solutions when using the explode function in PHP Common errors and solutions when using the explode function in PHP Mar 11, 2024 am 08:33 AM

Title: Common errors and solutions when using the explode function in PHP In PHP, the explode function is a common function used to split a string into an array. However, some common errors can occur due to improper use or incorrect data format. This article will analyze the problems you may encounter when using the explode function, and provide solutions and specific code examples. Mistake 1: The delimiter parameter is not passed in. When using the explode function, one of the most common mistakes is that the delimiter parameter is not passed in.

Split and merge strings using the explode and implode functions Split and merge strings using the explode and implode functions Jun 15, 2023 pm 08:42 PM

In PHP programming, processing strings is a frequently required operation. Among them, splitting and merging strings are two common requirements. In order to perform these operations more conveniently, PHP provides two very practical functions, namely the explode and implode functions. This article will introduce the usage of these two functions, as well as some practical skills. 1. The explode function The explode function is used to split a string according to the specified delimiter and return an array. Its function prototype is as follows: arra

Split string into array using PHP function 'explode' Split string into array using PHP function 'explode' Jul 24, 2023 pm 11:09 PM

Use the PHP function "explode" to split a string into an array. In PHP development, you often encounter situations where you need to split a string according to the specified delimiter. At this time, we can use PHP's built-in function "explode" to convert string to array. This article will introduce how to use the "explode" function to split a string and give relevant code examples. The basic syntax of the "explode" function is as follows: arrayexplode(

Use PHP function 'explode' to split a string into multiple substrings Use PHP function 'explode' to split a string into multiple substrings Jul 25, 2023 pm 06:29 PM

Use the PHP function "explode" to split a string into multiple substrings. In PHP programming, we often encounter situations where we need to split a string into multiple substrings. At this time, PHP provides a very convenient function "explode" that can help us easily implement this function. The syntax of the "explode" function is as follows: arrayexplode(string$delimiter,string$string[,in

What should I do if php explode reports an error? What should I do if php explode reports an error? Jan 18, 2023 am 10:13 AM

Solution to php explode error: 1. Find and open the erroneous PHP code; 2. Find the explode function part; 3. Modify the code to "dump(explode(',',$str));die;", that is, use Just comma-separate the array.

How to use explode function to split string in PHP How to use explode function to split string in PHP Jun 26, 2023 pm 12:03 PM

In the PHP language, there are many basic functions that can help us process strings quickly and efficiently. Among them, the explode function is a very practical string splitting function. It can split a string into arrays according to the specified delimiter, and then perform more flexible string operations. In this article, we will introduce how to use the explode function to split strings in PHP. 1. The format of the explode function. The format of the explode function in the PHP language is as follows: explode(separa

Timestamp handling in PHP: How to convert datetime to timestamp using strtotime function Timestamp handling in PHP: How to convert datetime to timestamp using strtotime function Jul 30, 2023 pm 04:19 PM

Timestamp processing in PHP: How to convert datetime to timestamp using strtotime function A timestamp is the number of seconds since January 1, 1970 00:00:00 (GMT) to the present. In PHP, we often need to convert between dates and times, making it easier for us to perform time operations. PHP provides the strtotime function to convert date time into a timestamp. Let's take a look at how to use the strtotime function correctly. First, let's look at

See all articles