Home > php教程 > PHP源码 > php 日期与日间之差函数

php 日期与日间之差函数

WBOY
Release: 2016-06-08 17:28:57
Original
1058 people have browsed it
<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);
?>

Related labels:
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 Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template