看到你,梁松远的博客

位置 » 主页 » 代码分享 » php友好时间展示,多少年多少月多少天

php友好时间展示,多少年多少月多少天

 /**
 * 友好时间展示,多少年多少月多少天
 * @param datetime 传入时间
 * @return string
 */
 
 function date_format_friendly($datetime){
 $now = time();
 $years = date('Y', $now) - date('Y', $datetime) - 1;
 if (date('m', $now) == date('m', $datetime)){
 
 if (date('d', $now) > date('d',$datetime)){
 $years++;
 $days = date('d', $now) - date('d',$datetime);
 $months = 0;
 }elseif(date('d', $now) == date('d',$datetime)){
 $days = 0;
 $months = 0;
 }else{
 if(in_array(date('d', $now),array(1,3,5,7,8,10,12))){
 $monthday = 31;
 }elseif(date('d', $now)==2){
 if(date('Y', $now)%4==0&&date('Y', $now)%100!=0||date('Y', $now)%400==0){
 $monthday = 29;
 }else{
 $monthday = 28;
 }
 }else{
 $monthday = 30;
 }
 $days = date('d', $now) - date('d',$datetime)+$monthday;
 $months = 12;
 }
 }elseif (date('m', $now) > date('m', $datetime)){
 $years++;
 if (date('d', $now) >= date('d',$datetime)){
 $days = date('d', $now) - date('d',$datetime);
 $months = date('m', $now) - date('m', $datetime);
 }else{
 if(in_array(date('d', $now),array(1,3,5,7,8,10,12))){
 $monthday = 31;
 }elseif(date('d', $now)==2){
 if(date('Y', $now)%4==0&&date('Y', $now)%100!=0||date('Y', $now)%400==0){
 $monthday = 29;
 }else{
 $monthday = 28;
 }
 }else{
 $monthday = 30;
 }
 $days = date('d', $now) - date('d',$datetime)+$monthday;
 $months = date('m', $now) - date('m', $datetime)-1;
 }
 }else{
 if (date('d', $now) >= date('d',$datetime)){
 $days = date('d', $now) - date('d',$datetime);
 $months = date('m', $now) - date('m', $datetime)+12;
 }else{
 if(in_array(date('d', $now),array(1,3,5,7,8,10,12))){
 $monthday = 31;
 }elseif(date('d', $now)==2){
 if(date('Y', $now)%4==0&&date('Y', $now)%100!=0||date('Y', $now)%400==0){
 $monthday = 29;
 }else{
 $monthday = 28;
 }
 }else{
 $monthday = 30;
 }
 $days = date('d', $now) - date('d',$datetime)+$monthday;
 $months = date('m', $now) - date('m', $datetime)+11;
 }
 }
 
 $str = '';
 if($years>0){
 $str .= $years."年";
 }
 if($months>0){
 $str .= $months."个月";
 }
 if($days>0){
 $str .= $days."天";
 }
 return $str;
 }