Heim  >  Artikel  >  Backend-Entwicklung  >  PHP 怎么计算年龄,域名年龄?已知 YYYY-mm-dd。求域名年龄

PHP 怎么计算年龄,域名年龄?已知 YYYY-mm-dd。求域名年龄

WBOY
WBOYOriginal
2016-06-13 11:09:571236Durchsuche

PHP 如何计算年龄,域名年龄?已知 YYYY-mm-dd。求域名年龄。
如何计算域名年龄困扰了很久,自己写了一个函数,但是是错误的,因为有的年份是闰年的。

<br /><?php <br /><br /><br />//时间输入必须单位<br />function mathAge($ymd2){<br />    $ymd1 = "2012-2-27";<br />    @list($y1,$m1,$d1) = explode("-",date("Y-m-d", strtotime($ymd1)));<br />    @list($y2,$m2,$d2) = explode("-",date("Y-m-d", strtotime($ymd2)));<br />    <br />    $arr = array(<br />    "年" =>  round( $y1-$y2 ),<br />    "月" =>  round( $m1-$m2 ),<br />    "天" =>  round( $d1-$d2 ),<br />    );<br /><br />    $arr["天"] = round($d1-$d2);<br />    while ($arr["天"] < 0) {<br />        $arr["天"] += 30;<br />        $arr["月"] -= 1;<br />    }<br />    <br />    while ($arr["月"] < 0) {<br />    	$arr["月"] += 12;<br />    	$arr["年"] -= 1;<br />    }<br />    $txt = '';<br />    foreach ($arr as $k => $v)<br />    {<br />        if(!$v)  continue;<br />        $txt .= $v.$k;<br />    }<br />    echo "$ymd1 - $ymd2 = $txt \n";<br />    return $txt;<br />  }<br /><br />  $dateArr = array(<br />  "2011-12-28",<br />  "2011-12-29",<br />  "2011-12-27",<br />  <br />  "2010-12-28",<br />  "2010-12-29",<br />  "2010-12-27",<br />  <br />  "2011-10-1",<br />  "2010-5-1",<br />  "2010-2-28",<br />  <br />  "1995-1-1",<br />  "1995-12-31",<br />  );<br />  foreach ($dateArr as $date)<br />  {<br />      mathAge($date);<br />      <br />  }<br />?><br /><br />


以上输出测试内容为:


2012-2-27 - 2011-12-28 = 1月29天 <br />2012-2-27 - 2011-12-29 = 1月28天 <br />2012-2-27 - 2011-12-27 = 2月 <br />2012-2-27 - 2010-12-28 = 1年1月29天 <br />2012-2-27 - 2010-12-29 = 1年1月28天 <br />2012-2-27 - 2010-12-27 = 1年2月 <br />2012-2-27 - 2011-10-1 = 4月26天 <br />2012-2-27 - 2010-5-1 = 1年9月26天 <br />2012-2-27 - 2010-2-28 = 1年11月29天 <br />2012-2-27 - 1995-1-1 = 17年1月26天 <br />2012-2-27 - 1995-12-31 = 16年1月26天 

如何计算好两个日期之间相差的年月?考虑闰年 2月份。
------解决方案--------------------
好吧,我也不是很清楚,不过特意过来顶一下楼主

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn