PHP时间类完整实例(非常实用)
本文实例讲述了PHP时间类。分享给大家供大家参考,具体如下:
<?php
header("Content-type:text/html;Charset=utf-8");
classtime{
private$year;//年
private$month;//月
private$day;//天
private$hour;//小时
private$minute;//分钟
private$second;//秒
private$microtime;//毫秒
private$weekday;//星期
private$longDate;//完整的时间格式
private$diffTime;//两个时间的差值
//返回年份time:时间格式为时间戳2013-3-27
functiongetyear($time="",$type=""){
if($time==""){
$time=time();
}
if($type==1){
return$this->year=date("y",$time);//返回两位的年份13
}else{
return$this->year=date("Y",$time);//返回四位的年份2013
}
}
//返回当前时间的月份time:时间格式为时间戳2013-3-27
functiongetmonth($time="",$type=""){
if($time==""){
$time=time();
}
switch($type){
case1:$this->month=date("n",$time);//返回格式3
break;
case2:$this->month=date("m",$time);//返回格式03
break;
case3:$this->month=date("M",$time);//返回格式Mar
break;
case4:$this->month=date("F",$time);//返回格式March
break;
default:$this->month=date("n",$time);
}
return$this->month;
}
//返回当前时间的天数time:时间格式为时间戳2013-3-4
functiongetday($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-101:19:2120:19:21
functiongethour($time="",$type=""){
if($time==""){
$time=time();
}
switch($type){
case1:$this->hour=date("H",$time);//格式:120
break;
case2:$this->hour=date("h",$time);//格式0108
break;
case3:$this->hour=date("G",$time);//格式120
break;
case4:$this->hour=date("g",$time);//格式18
break;
default:$this->hour=date("H",$time);
}
return$this->hour;
}
//返回当前时间的分钟数1:9:18
functiongetminute($time="",$type=""){
if($time==""){
$time=time();
}
$this->minute=date("i",$time);//格式09
return$this->minute;
}
//返回当前时间的秒数20:19:01
functiongetsecond($time="",$type=""){
if($time==""){
$time=time();
}
$this->second=date("s",$time);//格式01
return$this->second;
}
//返回当前时间的星期数
functiongetweekday($time="",$type=""){
if($time==""){
$time=time();
}
if($type==1){
$this->weekday=date("D",$time);//格式Sun
}elseif($type==2){
$this->weekday=date("l",$time);//格式Sunday
}else{
$this->weekday=date("w",$time);//格式数字表示0--6
}
return$this->weekday;
}
//比较两个时间的大小格式2013-3-48:4:3
functioncompare($time1,$time2){
$time1=strtotime($time1);
$time2=strtotime($time2);
if($time1>=$time2){//第一个时间大于等于第二个时间返回1否则返回0
return1;
}else{
return-1;
}
}
//比较两个时间的差值
functiondiffdate($time1="",$time2=""){
//echo$time1.'------'.$time2.'<br>';
if($time1==""){
$time1=date("Y-m-dH:i:s");
}
if($time2==""){
$time2=date("Y-m-dH: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);
$second=floor(($diff%60));
$this->diffTime='相差'.$day.'天'.$hour.'小时'.$minute.'分钟'.$second.'秒';
}
return$this->diffTime;
}
//返回X年X月X日
functionbuildDate($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程序设计有所帮助。