本帖最后由 huawei 于 2017-3-5 10:27 编辑
PHP如何实现,根据出生年月日计算生日(精确到xxxx年xx月xx天)?
<?php
function diffDate($date1,$date2){
$datestart= date('Y-m-d',strtotime($date1));
if(strtotime($datestart)>strtotime($date2)){
$tmp=$date2;
$date2=$datestart;
$datestart=$tmp;
}
list($Y1,$m1,$d1)=explode('-',$datestart);
list($Y2,$m2,$d2)=explode('-',$date2);
$Y=$Y2-$Y1; // 1
$m=$m2-$m1; // 0
$d=$d2-$d1; // -11
if($d<0){
$d+=(int)date('t',strtotime("-1 month $date2"));
$m=$m--;
}
if($m<0){
$m+=12;
$y=$y--;
}
if($Y == 0 && $m == 0 && $d != 0){
return $d.'天';
}elseif($Y == 0 && $m != 0 && $d != 0){
return $m.'个月'.$d.'天';
}elseif($Y != 0 && $m == 0 && $d != 0){
return $Y.'年'.$d.'天';
}else{
return $Y.'年'.$m.'个月'.$d.'天';
}
}
|
|