最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

php計(jì)算年齡精準(zhǔn)到年月日

 更新時(shí)間:2015年11月17日 14:21:41   投稿:lijiao  
這篇文章主要介紹了php計(jì)算年齡精準(zhǔn)到年月日的方法,涉及php操作日期與字符串的相關(guān)技巧,非常簡(jiǎn)單實(shí)用,需要的朋友可以參考下

本文實(shí)例講述了php計(jì)算年齡精準(zhǔn)到年月日的方法。分享給大家供大家參考。具體如下:

<?php
 
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
 
class Age {
   
  /**
   * 計(jì)算年齡精準(zhǔn)到年月日
   * @param type $birthday
   * @return array
   */
 
  public function calAge($birthday) {
    list($byear, $bmonth, $bday) = explode('-', $birthday);
    list($year, $month, $day) = explode('-', date('Y-m-d'));
    $bmonth = intval($bmonth);
    $bday = intval($bday);
    if ($bmonth < 10) {
      $bmonth = '0' . $bmonth;
    }
    if ($bday < 10) {
      $bday = '0' . $bday;
    }
    $bi = intval($byear . $bmonth . $bday);
    $ni = intval($year . $month . $day);
    $not_birth = 0;
    if ($bi > $ni) {
      $not_birth = 1;
      $tmp = array($byear, $bmonth, $bday);
      list($byear, $bmonth, $bday) = array($year, $month, $day);
      list($year, $month, $day) = $tmp;
      list($bi, $ni) = array($ni, $bi);
    }
    $years = 0;
    while (($bi + 10000) <= $ni) {//先取歲數(shù)
      $bi += 10000;
      $years++;
      $byear++;
    }//得到歲數(shù)后 拋棄年
    list($m, $d) = $this->getMD(array($year, $month, $day), array($byear, $bmonth, $bday));
    return array('year' => $years, 'month' => $m, 'day' => $d, 'not_birth' => $not_birth);
  }
 
  /**
   * 只能用于一年內(nèi)計(jì)算
   * @param type $ymd
   * @param type $bymd
   */
  public function getMD($ymd, $bymd) {
    list($y, $m, $d) = $ymd;
    list($by, $bm, $bd) = $bymd;
    if (($m . $d) < ($bm . $bd)) {
      $m +=12;
    }
    $month = 0;
    while ((($bm . $bd) + 100) <= ($m . $d)) {
      $bm++;
      $month++;
    }
    if ($bd <= $d) {//同處一個(gè)月
      $day = $d - $bd;
    } else {//少一個(gè)月
      $mdays = $bm > 12 ? $this->_getMothDay( ++$by, $bm - 12) : $this->_getMothDay($by, $bm);
      $day = $mdays - $bd + $d;
    }
    return array($month, $day);
  }
 
  private function _getMothDay($year, $month) {
    switch ($month) {
      case 1:
      case 3:
      case 5:
      case 7:
      case 8:
      case 10:
      case 12:
        $day = 31;
        break;
      case 2:
        $day = (intval($year % 4) ? 28 : 29); //能被4除盡的為29天其他28天
        break;
      default:
        $day = 30;
        break;
    }
    return $day;
  }
 
}
 
$cage = new Age();
$test = array(
  '1990-06-12',
  '1990-07-13',
  '1990-08-16',
  '1990-10-10',
  '1990-10-13',
  '1990-10-15',
  '1990-11-9',
  '1990-11-22',
  '2016-11-22',
  '2016-8-22',
  '2016-10-13',
);
echo date('Y-m-d');
echo '<pre>';
foreach($test as $v){
  $tmp = $cage->calAge($v);
  echo $v , ':', $tmp['year'], '年', $tmp['month'],
 '月', $tmp['day'], '天', ';', $tmp['not_birth'], '<br>';
}
echo '</pre>' ;
 
 
/*
  運(yùn)行結(jié)果:
  2015-10-13
  1990-06-12:25年4月1天;0
  1990-07-13:25年3月0天;0
  1990-08-16:25年1月27天;0
  1990-10-10:25年0月3天;0
  1990-10-13:25年0月0天;0
  1990-10-15:24年11月28天;0
  1990-11-9:24年11月4天;0
  1990-11-22:24年10月21天;0
  2016-11-22:1年1月9天;1
  2016-8-22:0年10月9天;1
  2016-10-13:1年0月0天;1
 
 * 
 */

希望本文所述對(duì)大家學(xué)習(xí)php程序設(shè)計(jì)有所幫助。

相關(guān)文章

  • PHP批量生成圖片縮略圖的方法

    PHP批量生成圖片縮略圖的方法

    這篇文章主要介紹了PHP批量生成圖片縮略圖的方法,涉及php針對(duì)圖片屬性操作的相關(guān)技巧,需要的朋友可以參考下
    2015-06-06
  • PHP實(shí)現(xiàn)通過(guò)strace定位故障原因的方法

    PHP實(shí)現(xiàn)通過(guò)strace定位故障原因的方法

    這篇文章主要介紹了PHP實(shí)現(xiàn)通過(guò)strace定位故障原因的方法,結(jié)合實(shí)例形式分析了出現(xiàn)高負(fù)載情況下使用strace定位故障原因的相關(guān)命令與操作技巧,需要的朋友可以參考下
    2018-04-04
  • PHP 日常開(kāi)發(fā)小技巧

    PHP 日常開(kāi)發(fā)小技巧

    在php的開(kāi)發(fā)過(guò)程中的一些小技巧,經(jīng)常能遇到的一些。需要的朋友可以參考下。
    2009-09-09
  • php實(shí)現(xiàn)的簡(jiǎn)單美國(guó)商品稅計(jì)算函數(shù)

    php實(shí)現(xiàn)的簡(jiǎn)單美國(guó)商品稅計(jì)算函數(shù)

    這篇文章主要介紹了php實(shí)現(xiàn)的簡(jiǎn)單美國(guó)商品稅計(jì)算函數(shù),涉及php數(shù)學(xué)計(jì)算的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-07-07
  • PHP使用PDO訪問(wèn)oracle數(shù)據(jù)庫(kù)的步驟詳解

    PHP使用PDO訪問(wèn)oracle數(shù)據(jù)庫(kù)的步驟詳解

    POD擴(kuò)展是在PHP5中加入,該擴(kuò)展提供PHP內(nèi)置類 PDO來(lái)對(duì)數(shù)據(jù)庫(kù)進(jìn)行訪問(wèn),不同數(shù)據(jù)庫(kù)使用相同的方法名,解決數(shù)據(jù)庫(kù)連接不統(tǒng)一的問(wèn)題。下面這篇文章主要給大家介紹了關(guān)于PHP使用PDO訪問(wèn)oracle數(shù)據(jù)庫(kù)的步驟,需要的朋友可以參考下。
    2017-09-09
  • php中return的用法實(shí)例分析

    php中return的用法實(shí)例分析

    這篇文章主要介紹了php中return的用法,實(shí)例分析了php中return的功能及常見(jiàn)的使用技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-02-02
  • php ActiveMQ的安裝與使用方法圖文教程

    php ActiveMQ的安裝與使用方法圖文教程

    這篇文章主要介紹了php ActiveMQ的安裝與使用方法,結(jié)合圖文與實(shí)例形式分析了ActiveMQ的功能、安裝、使用方法及操作注意事項(xiàng),需要的朋友可以參考下
    2020-02-02
  • 解析php dirname()與__FILE__常量的應(yīng)用

    解析php dirname()與__FILE__常量的應(yīng)用

    本篇文章是對(duì)php中的dirname()與__FILE__常量的應(yīng)用進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
    2013-06-06
  • PHP  Yii清理緩存的實(shí)現(xiàn)方法

    PHP Yii清理緩存的實(shí)現(xiàn)方法

    這篇文章主要介紹了PHP Yii清理緩存的實(shí)現(xiàn)方法的相關(guān)資料,這里舉例說(shuō)明如何實(shí)現(xiàn),需要的朋友可以參考下
    2016-11-11
  • php 指定范圍內(nèi)多個(gè)隨機(jī)數(shù)代碼實(shí)例

    php 指定范圍內(nèi)多個(gè)隨機(jī)數(shù)代碼實(shí)例

    在php中生成隨機(jī)數(shù)據(jù)我們可以使用rand,mt_rand都可以生成指定范圍內(nèi)隨機(jī)數(shù)據(jù)了,下面給各位同學(xué)介紹一下方法
    2016-07-07

最新評(píng)論

庆云县| 尤溪县| 高台县| 弥勒县| 桓仁| 红河县| 贺兰县| 汉沽区| 大冶市| 民权县| 会昌县| 香格里拉县| 平遥县| 天镇县| 磐石市| 乳山市| 永州市| 广东省| 栾川县| 交口县| 水富县| 兴隆县| 襄垣县| 玉门市| 通山县| 古田县| 石渠县| 鄂尔多斯市| 青海省| 徐闻县| 三原县| 县级市| 兰考县| 廊坊市| 鄱阳县| 武清区| 平阴县| 高淳县| 武城县| 泸定县| 申扎县|