您好,欢迎访问一九零五行业门户网

PHP时间类完整实例非常实用

本文实例讲述了php时间类。分享给大家供大家参考,具体如下:
year=date(y,$time); //返回两位的年份 13 }else{ return $this->year=date(y,$time); //返回四位的年份 2013 } } //返回当前时间的月份 time:时间格式为时间戳 2013-3-27 function getmonth($time=,$type=){ if($time==){ $time=time(); } switch($type){ case 1:$this->m//返回格式 3 break; case 2:$this->m//返回格式 03 break; case 3:$this->m//返回格式 mar break; case 4:$this->m//返回格式 march break; default:$this->m } return $this->month; } //返回当前时间的天数 time:时间格式为时间戳 2013-3-4 function getday($time=,$type=){ if($time==){ $time=time(); } if($type==1){ $this->day=date(d,$time);//返回格式 04 }else{ $this->day=date(j,$time);//返回格式 4 } return $this->day; } //返回当前时间的小时 2010-11-10 1:19:21 20:19:21 function gethour($time=,$type=){ if($time==){ $time=time(); } switch($type){ case 1:$this->hour=date(h,$time);//格式: 1 20 break; case 2:$this->hour=date(h,$time);//格式 01 08 break; case 3:$this->hour=date(g,$time);//格式 1 20 break; case 4:$this->hour=date(g,$time);//格式 1 8 break; default :$this->hour=date(h,$time); } return $this->hour; } //返回当前时间的分钟数 1:9:18 function getminute($time=,$type=){ if($time==){ $time=time(); } $this->minute=date(i,$time); //格式 09 return $this->minute; } //返回当前时间的秒数 20:19:01 function getsecond($time=,$type=){ if($time==){ $time=time(); } $this->sec //格式 01 return $this->second; } //返回当前时间的星期数 function getweekday($time=,$type=){ if($time==){ $time=time(); } if($type==1){ $this->weekday=date(d,$time);//格式 sun }else if($type==2){ $this->weekday=date(l,$time); //格式 sunday }else{ $this->weekday=date(w,$time);//格式 数字表示 0--6 } return $this->weekday; } //比较两个时间的大小 格式 2013-3-4 8:4:3 function compare($time1,$time2){ $time1=strtotime($time1); $time2=strtotime($time2); if($time1>=$time2){ //第一个时间大于等于第二个时间 返回1 否则返回0 return 1; }else{ return -1; } } //比较两个时间的差值 function diffdate($time1=,$time2=){ //echo $time1.'------'.$time2.'
'; if($time1==){ $time1=date(y-m-d h:i:s); } if($time2==){ $time2=date(y-m-d h:i:s); } $date1=strtotime($time1); $date2=strtotime($time2); if($date1>$date2){ $diff=$date1-$date2; }else{ $diff=$date2-$date1; } if($diff>=0){ $day=floor($diff/86400); $hour=floor(($diff%86400)/3600); $minute=floor(($diff%3600)/60); $sec $this->difftime='相差'.$day.'天'.$hour.'小时'.$minute.'分钟'.$second.'秒'; } return $this->difftime; } //返回 x年x月x日 function builddate($time=,$type=){ if($type==1){ $this->longdate = $this->getyear($time) . '年' . $this->getmonth($time) . '月' . $this->getday($time) . '日'; }else{ $this->longdate = $this->getyear($time) . '年' . $this->getmonth($time) . '月' . $this->getday($time) . '日'.$this->gethour($time).':'.$this->getminute($time).':'.$this->getsecond($time); } return $this->longdate; }}?>
希望本文所述对大家php程序设计有所帮助。
以上就介绍了php时间类完整实例非常实用,包括了方面的内容,希望对php教程有兴趣的朋友有所帮助。
其它类似信息

推荐信息