(Practical) PHP time class complete Example

Source: Internet
Author: User
Tags diff difference between two times
(Practical) PHP time class complete Example

<?phpheader ("content-type:text/html;  Charset=utf-8 "); class time{private $year;//private $month;//month private $day;//day private $hour;//hours private $minute;//Minutes private $second;//second private $microtime;//milliseconds private $weekday;//week private $longDate;//Full time format private $diffTime;// difference between two times//returns year time: Timestamp 2016-8-27 function getyear ($time = "", $type = "") {if ($time = = "") {$time =time ();} if ($type ==1) {return $this->year=date ("Y", $time);//return two-digit year}else{return $this->year=date ("Y", $time);//return four-bit year 2016}}/  /Returns the month of the current time time: The time format is timestamp 2016-8-27 function getmonth ($time = "", $type = "") {if ($time = = "") {$time =time ();} switch ($type) {  Case 1: $this->month=date ("n", $time);//return format 8 break;  Case 2: $this->month=date ("M", $time);//return format;  Case 3: $this->month=date ("M", $time);//return format;  Case 4: $this->month=date ("F", $time);//return format August break; Default: $this->month=date ("n", $time);  } return $this->month; }//Returns the number of days of the current time: The time format is timestamp 2016-8-29 function GetdaY ($time = "", $type = "") {if ($time = = ") {$time =time ();} if ($type ==1) {$this->day=date (" D ", $time);//return format}else{$thi S->day=date ("J", $time);//return format: $this->day;  }//Returns the hour of the current time 2016-08-10 1:19:21 20:19:21 function gethour ($time = "", $type = "") {if ($time = = "") {$time =time ();}  Switch ($type) {Case 1: $this->hour=date ("H", $time);//format: 1 break;  Case 2: $this->hour=date ("H", $time);//format;  Case 3: $this->hour=date ("G", $time);//format 1 break;   Case 4: $this->hour=date ("G", $time);//format 1 8 break; Default: $this->hour=date ("H", $time); } return $this->hour; }//Returns the number of minutes of the current time 1:9:18 function getminute ($time = "", $type = "") {if ($time = = "") {$time =time ();} $this->minute=date ("I", $time); Format return $this->minute; }//Returns the number of seconds in the current time 20:19:01 function Getsecond ($time = "", $type = "") {if ($time = = "") {$time =time ();} $this->second=date ("s" , $time); Format->second; return $this }//Returns the number of weeks of the current time function getweekday ($time = "", $type = "") {if ($time== "") {$time =time (); if ($type ==1) {$this->weekday=date ("D", $time),//Format Sun}else if ($type ==2) {$this->weekday=date ("L", $time); Format Sunday}else{$this->weekday=date ("W", $time);//format number means 0--6} return $this->weekday; }//Compare two time size formats 2016-8-27 8:4:3 function Compare ($time 1, $time 2) {$time 1=strtotime ($time 1); $time 2=strtotime ($time 2); I F ($time 1>= $time 2) {//The first time is greater than or equal to the second time returns 1 otherwise returns 0 return 1;} else{return-1;}} Compare two time difference function diffdate ($time 1= "", $time 2= "") {//echo $time 1. '------'. $time 2. ' <br> ';  if ($time 1== "") {$time 1=date ("y-m-d h:i:s");  } if ($time 2== "") {$time 2=date ("y-m-d h:i:s"); } $date 1=strtotime ($time 1); $date 2=strtotime ($time 2);  if ($date 1> $date 2) {$diff = $date 1-$date 2;  }else{$diff = $date 2-$date 1;} if ($diff >=0) {$day =floor ($diff/86400);  $hour =floor (($diff%86400)/3600);  $minute =floor (($diff%3600)/60);  $second =floor (($diff%60));  $this->difftime= ' difference '. $day. ' Day '. $hour. ' Hours '. $minute. ' minutes '. $second. ' Seconds '; } return $this->difftime; }//returns x year x month x Day function builddate ($time = "", $type = "") {if ($type ==1) {$this->longdate = $this->getyear ($time). ' Year '. $this->getmonth ($time). ' Month '. $this->getday ($time).   ' Day '; }else{$this->longdate = $this->getyear ($time). ' Year '. $this->getmonth ($time). ' Month '. $this->getday ($time).  ' Day '.   $this->gethour ($time). ': '. $this->getminute ($time). ': '. $this->getsecond ($time);   } return $this->longdate; }}?>

The above is (practical article) PHP time class Complete instance of the content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.