网站首页  汉语字词  英语词汇  考试资料  写作素材  旧版资料

请输入您要查询的考试资料:

 

标题 PHP计算两个时间的差(秒分时天月年)
内容
    两个时间之间月份差实例代码:
    代码如下:
    $yourdate="2012-10-20";
    $yourdate_unix=strtotime($yourdate);
    echo (date("Y",$yourdate_unix)-date("Y"))*12+(date("m",$yourdate_unix)-date("m"));
    例子1
    代码如下:
    /*
    * 计算2个时间段的月份差
    * @param $st开始时间 $et结束时间(时间戳格式)
    * @return $total 返回的差值
    */
    function getMonthNum($st, $et)
    {
    $s_m = date('n', $st);
    $e_m = date('n', $et);
    $s_y = date('Y', $st);
    $e_y = date('Y', $et);
    $total = 13 - $s_m + ($e_y - $s_y - 1) * 12 + $e_m; //计算月份差
    return $total;
    }
    例子2
    代码如下:
    <?php
    $one = strtotime('2011-05-08 07:02:40');//开始时间 时间戳
    $tow = strtotime('2012-12-25 00:00:00');//结束时间 时间戳
    $cle = $tow - $one; //得出时间戳差值
    /* 这个只是提示
    echo ceil($cle/60); //得出一共多少分钟
    echo ceil($cle/3600); //得出一共多少小时
    echo ceil($cle/3600/24); //得出一共多少天
    */
    /*ceil()函数,即进一法取整*/
    $d = cell($cle/3600/24);
    $h = cell(($cle%(3600*24))/3600); //%取余
    $m = cell(($cle%(3600*24))/60);
    echo "两个时间相差 $d 天 $h 小时 $m 分"
    ?>
    例子3
    代码如下:
    <?PHP
    /*
    *
    *函数功能:计算两个以YYYY-MM-DD为格式的日期,相差几天
    *
    */
    function getChaBetweenTwoDate($date1,$date2){
    $Date_List_a1=explode("-",$date1);
    $Date_List_a2=explode("-",$date2);
    $d1=mktime(0,0,0,$Date_List_a1[1],$Date_List_a1[2],$Date_List_a1[0]);
    $d2=mktime(0,0,0,$Date_List_a2[1],$Date_List_a2[2],$Date_List_a2[0]);
    $Days=round(($d1-$d2)/3600/24);
    return $Days;
    }
    echo getChaBetweenTwoDate('2010-08-11','2010-08-16');
    echo "<br>";
    echo getChaBetweenTwoDate('2010-08-16','2010-08-11');
    ?>
    例子4
    代码如下:
    <?php
    $startdate=”2010-12-11 11:40:00″;
    $enddate=”2012-12-12 11:45:09″;
    $date=floor((strtotime($enddate)-strtotime($startdate))/86400);
    $hour=floor((strtotime($enddate)-strtotime($startdate))%86400/3600);
    $minute=floor((strtotime($enddate)-strtotime($startdate))%86400/60);
    $second=floor((strtotime($enddate)-strtotime($startdate))%86400%60);
    echo $date.”天<br>”;
    echo $hour.”小时<br>”;
    echo $minute.”分钟<br>”;
    echo $second.”秒<br>”;
    ?>
随便看

 

在线学习网考试资料包含高考、自考、专升本考试、人事考试、公务员考试、大学生村官考试、特岗教师招聘考试、事业单位招聘考试、企业人才招聘、银行招聘、教师招聘、农村信用社招聘、各类资格证书考试等各类考试资料。

 

Copyright © 2002-2024 cuapp.net All Rights Reserved
更新时间:2025/5/15 7:00:06