zoukankan      html  css  js  c++  java
  • PHP计算两个时间之差的函数(年,月,周,日,小时,分钟,秒数)

     1 <? PHP
     2 //  时间格式为YYYYMMDDHHmmss

     3 function  timeDiff( $aTime , $bTime )
     4
    {
     5 // 分割第一个时间

     6      $ayear   =   substr ( $aTime , 0 , 4 );
     7      $amonth   =   substr ( $aTime , 4 , 2
    );
     8      $aday   =   substr ( $aTime , 6 , 2
    );
     9      $ahour   =   substr ( $aTime , 8 , 2
    );
    10      $aminute   =   substr ( $aTime , 10 , 2
    );
    11      $asecond   =   substr ( $aTime , 12 , 2
    );
    12 // 分割第二个时间

    13      $byear   =   substr ( $bTime , 0 , 4 );
    14     $bmonth   =   substr ( $bTime , 4 , 2
    );
    15     $bday   =   substr ( $bTime , 6 , 2
    );
    16     $bhour   =   substr ( $bTime , 8 , 2
    );
    17     $bminute   =   substr ( $bTime , 10 , 2
    );
    18    $bsecond   =   substr ( $bTime , 12 , 2
    );
    19 // 生成时间戳

    20    $a   =   mktime ( $ahour , $aminute , $asecond , $amonth , $aday , $ayear );
    21    $b   =   mktime ( $bhour , $bminute , $bsecond , $bmonth , $bday , $byear
    );
    22    $timeDiff [ ' second ' =   $a - $b
    ;
    23 // 采用了四舍五入,可以修改

    24   $timeDiff [ ' mintue ' =   round ( $timeDiff [ ' second ' ] / 60 );
    25   $timeDiff [ ' hour ' =   round ( $timeDiff [ ' mintue ' ] / 60
    );
    26   $timeDiff [ ' day ' =   round ( $timeDiff [ ' hour ' ] / 24
    );
    27   $timeDiff [ ' week ' =   round ( $timeDiff [ ' day ' ] / 7
    );
    28   $timeDiff [ ' month ' =   round ( $timeDiff [ ' day ' ] / 30 );  // 按30天来算

    29   $timeDiff [ ' year ' =   round ( $timeDiff [ ' day ' ] / 365 );  // 按365天来算 
    30   return   $timeDiff ;
    31
    }
    32 ?>
    用法:
    $a=timeDiff("20060613161000","20040613150000");
    print_r($a);

    返回一个数组,可以用下标直接输出来年,月,周,日,小时,分钟,秒数之差
  • 相关阅读:
    Jzoj2682 最长双回文串
    Jzoj2682 最长双回文串
    【hdu3853】Loops
    【tyvj1015】【caioj1060】公路乘车
    【luogu1064】金明的预算方案
    【bzoj1260】【CQOI2007】涂色paint
    【UVa1629】Cake slicing
    【NYOJ746】整数划分(四)
    【NYOJ 15】括号匹配2
    【poj2955】Brackets
  • 原文地址:https://www.cnblogs.com/studio313/p/1059211.html
Copyright © 2011-2022 走看看