search
Homephp教程PHP源码php 日期与日间之差函数

<script>ec(2);</script>

求二两日期之差函数

$time1 = "2008-6-15 11:49:59";//第一个时间
$time2 = "2007-5-5 12:53:28";//第二个时间
$t1 = strtotime($time1);
$t2 = strtotime($time2);
$t12 = abs($t1-$t2);
$start = 0;
$string = "两个时间相差:";
$y = floor($t12/(3600*24*360));
if($start || $y )
{
$start = 1;
$t12 -= $y*3600*24*360;
$string .= $y."年";
}
$m = floor($t12/(3600*24*31));
if($start || $m)
{
$start = 1;
$t12 -= $m*3600*24*31;
$string .= $m."月";
}
$d = floor($t12/(3600*24));
if($start || $d)
{
$start = 1;
$t12 -= $d*3600*24;
$string .= $d."天";
}
$h = floor($t12/(3600));
if($start || $h)
{
$start = 1;
$t12 -= $h*3600;
$string .= $h."时";
}
$s = floor($t12/(60));
if($start || $s)
{
$start = 1;
$t12 -= $s*60;
$string .= $s."分";
}
$string .= "{$t12}秒";
echo $string;
?>

这是一个求任意时间之差的函数


#作者:仙乐
#功能:获得任意时间与当前时间的时间差
function QueryDays($datestr){
#格式化时间
    $da=preg_split("/(-| |:)/i",$datestr);
    $nowyear=date("Y");
    $nowmon=date("n");
    $nowday=date("d");
    $nowtimes=mktime(0,0,0,$nowmon,$nowday,$nowyear);
    $pdtimes= mktime(0,0,0,$nowmon,$nowday,$nowyear-1);
    $bjtimes= mktime(0,0,0,$da[1],$da[2],$da[0]);
#判断所给出的时间是不是在一年内
    if ($bjtimes>=$pdtimes and $bjtimes         return (floor(strftime("%j",mktime(0,0,0,$nowmon,$nowday,$nowyear)-mktime($da[3],$da[4],$da[5],$da[1],$da[2],$da[0]))));
    }else{
        $loop=$nowyear-$da[0];
        $totaldays=(floor(strftime("%j",mktime(0,0,0,$nowmon,$nowday,$nowyear)-mktime(0,0,0,1,1,$nowyear))));
        for($i=1;$i             for($j=12;$j>=1;$j--){
                if ($da[0]==$nowyear-$i and $da[1]==$j){
                    $days=MonDays($nowyear-$i,$j);
                    return $totaldays+=$days-$da[2];
                    break;
                }else{
                    $days=MonDays($nowyear-$i,$j);
                    $totaldays+=$days;
                }//end else
             }//end for
        }//end for
    }//end else
}//end function
#取得月分的天数
function MonDays($year,$month){
    switch ($month){
        case "1":
        case "3":
        case "5":
        case "7":
        case "8":
        case "10":
        case "12": $days=31;break;
        case "4":
        case "6":
        case "9":
        case "11": $days=30;break;
        case "2": 
            if (checkdate($month,29,$year)){
                $days=29;
            }else{
                $days=28;
            }//end else
        break;
    }//end switch
    return $days;
}//end function
$datestr="2002-1-14 9:47:20";
echo QueryDays($datestr);
?>

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. Have Crossplay?
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

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.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools