Home  >  Article  >  Daily Programming  >  Several common time interval information acquisition methods in PHP

Several common time interval information acquisition methods in PHP

XuPing.Yang
XuPing.YangOriginal
2021-08-16 09:39:31483browse

Sometimes when we develop projects, we will encounter the start and end times of a few days ago, a few days later, today, this week, etc., get the previous months, get the number of days in this year, etc., the following is what I Summarized several common time-related methods, I hope they are useful to everyone:

1. Get the previous months

public function toSelfMonth($ m){
$today = input('param.today') ? input('param.today') : date("Y-m-d");
$arr = array();
$old_time = strtotime('-'.$m.' month',strtotime($today));
for($i = 0;$i <= $m; $i){
$t = strtotime( " $i month",$old_time);
$arr[]=date('Y-m',$t);
}
return $arr;
}

2. Get the date of the previous few days

public function toSelfDay($m){
$today = input('param .today') ? input('param.today') : date("Y-m-d");
$arr = array();
$old_time = strtotime('-'.$m.' day', strtotime($today));
for($i = 0;$i <= $m; $i){
$t = strtotime(" $i day",$old_time);
$arr[]=date('Y-m-d',$t);
}
return $arr;
}

## 3. Calculate the number of days in the previous months

public function getLastMonthDays($month){

$arr = $this->toSelfMonth($month);
$y = date ('Y',time());
$days = date('d',time());
for ($i=0;$i $days = $days date('t', strtotime($arr[$i]."-01"));
}
return $days;
}

4. Get the number of days in a certain monthpublic function getSelfMonthDays(){
/*$days = cal_days_in_month(CAL_GREGORIAN, 4, 2011);*/
$days = date( 't', strtotime("2011-4-1"));
/*$days = date("t");
echo "The number of days in the current month".$days."
";*/
return $days;
}

5. Get the start and end timestamps of the specified year and month

public function getSelfMonthStartEnd($y=0,$m=0){

$y = $y ? $y : date('Y');
$m = $m ? $m : date('m ');
$d = date('t', strtotime($y.'-'.$m));
return array("start"=>strtotime($y.'-'. $m),"end"=>mktime(23,59,59,$m,$d,$y));
}

6. Get the date interval

To get the time interval, you must first convert the time into a timestamp. Timestamps are generally measured in seconds, so the following measurement units are obtained:

(1) One hour is 3600s

(2) One day is 24*3600s, that is, 86400 seconds

According to these two conditions, the following method is obtained

function GetDateLong ($time1,$time2){

if($time2 >= $time1){

$long = $time2 - $time1;
}else{
$long = - ($time2 - $time1);
}
if($long > 0){
$year_day = $this->GetYearDay($time1);
$year = floor($ long/($year_day*86400));
$day = floor(($long)%($year_day*86400)/86400);
if($year >= 1){
return $ Year. 'Nian'. $ day. 'Heaven';
} else {
Return $ Day.
}


}


7. Get whether it is a leap year

If there are 365 days in a year, yes In ordinary years, when there are 366 days in a year, it is a leap year.
1. Ordinary leap year: The Gregorian calendar year is a multiple of 4, usually a leap year. (For example, 2004 is a leap year); 2. Century leap year: The Gregorian calendar year is a hundred, and it must be a multiple of 400 to be a leap year (for example, 1900 is not a century leap year, but 2000 is a century leap year).

According to this condition, we can get the following method to get whether it is a normal year or a leap year

public function GetYearType($year){

if ($year%4==0&&($year 0!=0 || $year@0==0)){

return 1;

}else{

return 2;
}
}




8. Determine how many days there are in a year

Same as the introduction of 7 above. The Gregorian calendar year is a multiple of 4, usually a leap year. Years in the Gregorian calendar are counted in hundreds and must be a multiple of 400 to be a leap year.

According to this condition, we can complete the method to get the number of days in a year

public function GetYearDay($time){
$year = date("Y",$time);
$a1 = $year/100;
if($a1 == 0){
$b1 = $year/400;
if($b1 > 0){
$ long = 365;
      }else{
                $long = 366;
                                                                                                                                                   . {
$long = 366;

#

The above is the detailed content of Several common time interval information acquisition methods in PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn