zoukankan      html  css  js  c++  java
  • PHP 求两个日期之间相差的天数、月数

    <?php
    /**
     * 求两个日期之间相差的天数
     * (针对1970年1月1日之后,求之前可以采用泰勒公式)
     * @param string $day1
     * @param string $day2
     * @return number
     */
    function diffBetweenTwoDays ($day1, $day2)
    {
      $second1 = strtotime($day1);
      $second2 = strtotime($day2);
        
      if ($second1 < $second2) {
        $tmp = $second2;
        $second2 = $second1;
        $second1 = $tmp;
      }
      return ($second1 - $second2) / 86400;
    }
    $day1 = "2013-07-27";
    $day2 = "2013-08-04";
    $diff = diffBetweenTwoDays($day1, $day2);
    echo $diff."
    ";
    /**
     * 返回相差的月份数量
     * @param $date1 string 开始日期
     * @param $date2 string 结束日期
     * @return float|int
     */
    function getMonthNum($date1,$date2){
        $date1_stamp=strtotime($date1);
        $date2_stamp=strtotime($date2);
        list($date_1['y'],$date_1['m'])=explode("-",date('Y-m',$date1_stamp));
        list($date_2['y'],$date_2['m'])=explode("-",date('Y-m',$date2_stamp));
        return abs($date_1['y']-$date_2['y'])*12 +$date_2['m']-$date_1['m'];
    }
  • 相关阅读:
    Node.js Express框架
    Node.js Web模块
    Node.js 工具模块
    Node.js GET/POST请求
    Node.js 文件系统
    Node.js 常用工具
    【day03】Xhtml
    【day02】Xhtml
    【紫书】【重要】Not so Mobile UVA
    【紫书】Tree UVA
  • 原文地址:https://www.cnblogs.com/liangzia/p/11662240.html
Copyright © 2011-2022 走看看