PHP Time Class Complete example is very practical

Source: Internet
Author: User
Tags difference between two times
The example in this article describes the PHP time class. Share to everyone for your reference, as follows:

<?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 2013-3-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 2013}}/  /Returns the month of the current time time: The time format is timestamp 2013-3-27 function getmonth ($time = "", $type = "") {if ($time = = "") {$time =time ();} switch ($type) {  Case 1: $this->m//return format 3 break;  Case 2: $this->m//return format;  Case 3: $this->m//return format Mar break;  Case 4: $this->m//return format March break;  Default: $this->m} return $this->month; }//Returns the number of days of the current time time: The time format is timestamp 2013-3-4 function getday ($time = "", $type = "") {if ($time = = "") {$time =time ();} if ($type ==1) {$ This->day=date ("D", $time);//return format 04}else{$this->day=date ("J", $time);//Return Format 4} return $this->day;  }//Returns the hour of the current time 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);//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->sec//Format Retu RN $this->second;  }//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 2013-3-4 8:4:3 function Compare ($time 1, $time 2) {$time 1=strtotime ($time 1); $time 2=strtotime ($time 2); if ($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. '
'; 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); $sec $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; }}?>

I hope this article is helpful to you in PHP programming.

The above describes the PHP time class complete example is very practical, including the aspects of the content, I hope that the PHP tutorial interested in a friend helpful.

  • 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.