PHP把时间转换成几分钟前、几小时前、几天前的几个函数、类分享 转
这篇文章主要介绍了php计算时间几分钟前、几小时前、几天前的几个函数、类分享,需要的朋友可以参考下
一、函数实现
实例1:
1 <?php 2 header("Content-type: text/html; charset=utf8"); 3 date_default_timezone_set("Asia/Shanghai"); //设置时区 4 function time_tran($the_time) { 5 $now_time = date("Y-m-d H:i:s", time()); 6 //echo $now_time; 7 $now_time = strtotime($now_time); 8 $show_time = strtotime($the_time); 9 $dur = $now_time - $show_time; 10 if ($dur < 0) { 11 return $the_time; 12 } else { 13 if ($dur < 60) { 14 return $dur . '秒前'; 15 } else { 16 if ($dur < 3600) { 17 return floor($dur / 60) . '分钟前'; 18 } else { 19 if ($dur < 86400) { 20 return floor($dur / 3600) . '小时前'; 21 } else { 22 if ($dur < 259200) {//3天内 23 return floor($dur / 86400) . '天前'; 24 } else { 25 return $the_time; 26 } 27 } 28 } 29 } 30 } 31 } 32 33 34 echo time_tran("2014-7-8 19:22:01"); 35 ?>
实例2:
1 <?php 2 function time_tranx($the_time){ 3 $now_time = date("Y-m-d H:i:s",time()+8*60*60); 4 $now_time = strtotime($now_time); 5 $show_time = strtotime($the_time); 6 $dur = $now_time - $show_time; 7 if($dur < 0){ 8 return $the_time; 9 }else{ 10 if($dur < 60){ 11 return $dur.'秒前'; 12 }else{ 13 if($dur < 3600){ 14 return floor($dur/60).'分钟前'; 15 }else{ 16 if($dur < 86400){ 17 return floor($dur/3600).'小时前'; 18 }else{ 19 if($dur < 259200){ //3天内 20 return floor($dur/86400).'天前'; 21 }else{ 22 return $the_time; 23 } 24 } 25 } 26 } 27 } 28 } 29 echo time_tranx("2014-7-8 19:22:01"); 30 ?>
实例3:
1 <?php 2 function format_date($time){ 3 $t=time()-$time; 4 <span style="white-space:pre"> </span>//echo time(); 5 $f=array( 6 '31536000'=>'年', 7 '2592000'=>'个月', 8 '604800'=>'星期', 9 '86400'=>'天', 10 '3600'=>'小时', 11 '60'=>'分钟', 12 '1'=>'秒' 13 ); 14 foreach ($f as $k=>$v) { 15 if (0 !=$c=floor($t/(int)$k)) { 16 return $c.$v.'前'; 17 } 18 } 19 } 20 echo format_date("1404600000"); 21 ?>
实例4:
1 <?php 2 function formatTime($date) { 3 $str = ''; 4 $timer = strtotime($date); 5 $diff = $_SERVER['REQUEST_TIME'] - $timer; 6 $day = floor($diff / 86400); 7 $free = $diff % 86400; 8 if($day > 0) { 9 return $day."天前"; 10 }else{ 11 if($free>0){ 12 $hour = floor($free / 3600); 13 $free = $free % 3600; 14 if($hour>0){ 15 return $hour."小时前"; 16 }else{ 17 if($free>0){ 18 $min = floor($free / 60); 19 $free = $free % 60; 20 if($min>0){ 21 return $min."分钟前"; 22 }else{ 23 if($free>0){ 24 return $free."秒前"; 25 }else{ 26 return '刚刚'; 27 } 28 } 29 }else{ 30 return '刚刚'; 31 } 32 } 33 }else{ 34 return '刚刚'; 35 } 36 } 37 } 38 echo formatTime("2014-7-8 19:22:01"); 39 ?>
二、类的实现
1 <?php 2 /* 3 * author: china_skag 4 * time: 2014-07-08 5 * 发博时间计算(年,月,日,时,分,秒) 6 * $createtime 可以是当前时间 7 * $gettime 你要传进来的时间 8 */ 9 class Mygettime{ 10 function __construct($createtime,$gettime) { 11 $this->createtime = $createtime; 12 $this->gettime = $gettime; 13 } 14 function getSeconds() 15 { 16 return $this->createtime-$this->gettime; 17 } 18 function getMinutes() 19 { 20 return ($this->createtime-$this->gettime)/(60); 21 } 22 function getHours() 23 { 24 return ($this->createtime-$this->gettime)/(60*60); 25 } 26 function getDay() 27 { 28 return ($this->createtime-$this->gettime)/(60*60*24); 29 } 30 function getMonth() 31 { 32 return ($this->createtime-$this->gettime)/(60*60*24*30); 33 } 34 function getYear() 35 { 36 return ($this->createtime-$this->gettime)/(60*60*24*30*12); 37 } 38 function index() 39 { 40 if($this->getYear() > 1) 41 { 42 if($this->getYear() > 2) 43 { 44 return date("Y-m-d",$this->gettime); 45 exit(); 46 } 47 return intval($this->getYear())." 年前"; 48 exit(); 49 } 50 if($this->getMonth() > 1) 51 { 52 return intval($this->getMonth())." 月前"; 53 exit(); 54 } 55 if($this->getDay() > 1) 56 { 57 return intval($this->getDay())." 天前"; 58 exit(); 59 } 60 if($this->getHours() > 1) 61 { 62 return intval($this->getHours())." 小时前"; 63 exit(); 64 } 65 if($this->getMinutes() > 1) 66 { 67 return intval($this->getMinutes())." 分钟前"; 68 exit(); 69 } 70 if($this->getSeconds() > 1) 71 { 72 return intval($this->getSeconds()-1)." 秒前"; 73 exit(); 74 } 75 } 76 } 77 //类的使用实例 78 /* 79 * 80 * 调用类输出方式 81 * 82 * $a = new Mygettime(time(),strtotime('-25 month')); 83 * echo iconv('utf-8', 'gb2312', $a->index())?iconv('utf-8', 'gb2312', $a->index()):iconv('utf-8', 'gb2312', '当前'); 84 * 85 */
转自:http://blog.csdn.net/china_skag/article/details/37569505