PHP Time Class Complete instance (very practical) _php tips

Source: Internet
Author: User

This example describes the PHP time class. Share to everyone for your reference, specific as follows:

<?php header ("content-type:text/html;
Charset=utf-8 "); Class time{Private $year//year private $month//month private $day,/day private $hour,/hour private $minute,/minute private $s econd;//Second Private $microtime//MS Private $weekday//week private $longDate//Full time format private $diffTime;//two time difference/return year
 Time format: Time stamp 2013-3-27 function getyear ($time = "", $type = "") {if ($time = = "") {$time =time (); } if ($type ==1) {return $this->year=date ("Y", $time);//Returns a two-digit year of}else{back $this->year=date ("Y", $time); Returns four-bit year 2013}//returns the current time month: Time format is timestamp 2013-3-27 function getmonth ($time = "", $type = "") {if ($time = = ") {$time =tim
 E ();
  Switch ($type) {Case 1: $this->month=date ("n", $time);//return format 3 break;
  Case 2: $this->month=date ("M", $time);//return format break;
  Case 3: $this->month=date ("M", $time);//return format Mar break;
  Case 4: $this->month=date ("F", $time);//return format March break;
 Default: $this->month=date ("n", $time); 
 return $this->month; }//Returns the current timeNumber of days: 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}else{$this->day=date ("J", $time);//Return Format 4} returns $th
 is->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 break;
  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 for the current time 1:9:18 function getminute ($time = "", $type = "") {if ($time = = "") {$time =time (); } $this->minute=date ("i", $time);
 Format $this->minute;
 //Returns the number of seconds for the current time 20:19:01 function Getsecond ($time = "", $type = "") {if ($time = = "") {$time =time (); } $this->second=date ("s", $time); Format rEturn $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 indicates 0--6} return $this->weekday;
 ///Compare two-time size format 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. '
 <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. ' Days ' $hour. ' Hours '. $minute. ' minute '. $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 will help you with your PHP programming.

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.