Home  >  Article  >  Backend Development  >  php gets some time to implement method practice

php gets some time to implement method practice

不言
不言Original
2018-04-13 14:03:031234browse

The content shared with you in this article is about the implementation method and practice of obtaining some time in PHP. It has a certain reference value. Friends in need can refer to it.

I have encountered it during development these days. I have sorted out some time issues and shared them with everyone. You can take a look and use them if necessary.

1. Get the first and last day of last month.

   echo date('Y-m-01', strtotime('-1 month'));   
   echo "
"; echo date('Y-m-t', strtotime('-1 month')); echo "
";

There are some problems with the above method, please make the following modifications:

Previous The first day of the month:
 

echo date('Y-m-d', strtotime(date('Y-m-01') . ' -1 month'));
// 计算出本月第一天再减一个月

The last day of the previous month:
 

echo date('Y-m-d', strtotime(date('Y-m-01') . ' -1 day')); 
// 计算出本月第一天再减一天

2. Get the first and last day of the month.

   $BeginDate=date('Y-m-01', strtotime(date("Y-m-d")));   
   echo $BeginDate;   echo "
"; echo date('Y-m-d', strtotime("$BeginDate +1 month -1 day")); echo "
";

3. Get the year, month, day and number of days of the current day.

   echo " 本月共有:".date("t")."天";   
   echo " 当前年份".date('Y');   
   echo " 当前月份".date('m');   
   echo " 当前几号".date('d');   
   echo "
";

4. Use functions and arrays to get the first and last days of the month and compare Practical

  function getthemonth($date){
       $firstday = date('Y-m-01', strtotime($date));       
       $lastday = date('Y-m-d', strtotime("$firstday +1 month -1 day"));       
       return array($firstday,$lastday);
  }   
  $today = date("Y-m-d");   
  $day=getthemonth($today);   
  echo "当月的第一天: ".$day[0]." 当月的最后一天: ".$day[1];  
  echo "
";

Get the date of this month:

function getMonth($date){
      $firstday = date("Y-m-01",strtotime($date));      
      $lastday = date("Y-m-d",strtotime("$firstday +1 month -1 day"));      
      return array($firstday,$lastday);
}

 $firstday is the first day of the month, if$date If is 2018-2, $firstday will be 2018-02-01, and then add one month based on $firstday to get 2018-03-01, minus one day is 2018-02-28, it is so convenient to use date() and strtotime().

Get the date of the previous month:

function getlastMonthDays($date){      
$timestamp=strtotime($date);      
$firstday=date('Y-m-01',strtotime(date('Y',$timestamp).'-'.(date('m',$timestamp)-1).'-01'));      
$lastday=date('Y-m-d',strtotime("$firstday +1 month -1 day"));      return array($firstday,$lastday);
}

The date of the previous month needs to get a timestamp first, and then -1 on the month OK, the super smart date() will convert things like 2018-0-1 into 2017-12-01, so cool .

Get the date of the next month:

function getNextMonthDays($date){      
$timestamp=strtotime($date);      
$arr=getdate($timestamp);      
if($arr['mon'] == 12){            
$year=$arr['year'] +1;            
$month=$arr['mon'] -11;            
$firstday=$year.'-0'.$month.'-01';            
$lastday=date('Y-m-d',strtotime("$firstday +1 month -1 day"));
      }else{            
      $firstday=date('Y-m-01',strtotime(date('Y',$timestamp).'-'.(date('m',$timestamp)+1).'-01'));            
      $lastday=date('Y-m-d',strtotime("$firstday +1 month -1 day"));
      }      
      return array($firstday,$lastday);
}

The code for the date of the next month looks a little longer, because date() cannot be converted to something like 2018-13-01 This kind of thing will go back directly to 1970, so we need to deal with the issue of 12 months before, except 12 Just enter month 1 and it will be OK.

Generally speaking, the date function is too powerful.

I have sorted out some of the time problems I encountered during development these days and shared them with everyone. You can take a look and use them if necessary.

1. Get the first and last day of last month.

   echo date('Y-m-01', strtotime('-1 month'));   
   echo "
"; echo date('Y-m-t', strtotime('-1 month')); echo "
";

There are some problems with the above method, please make the following modifications:

Previous The first day of the month:
 

echo date('Y-m-d', strtotime(date('Y-m-01') . ' -1 month'));// 计算出本月第一天再减一个月

The last day of the previous month:
 

echo date('Y-m-d', strtotime(date('Y-m-01') . ' -1 day')); 
// 计算出本月第一天再减一天

2. Get the first and last day of the month.

   $BeginDate=date('Y-m-01', strtotime(date("Y-m-d")));   
   echo $BeginDate;   echo "
"; echo date('Y-m-d', strtotime("$BeginDate +1 month -1 day")); echo "
";

3. Get the year, month, day and number of days of the current day.

   echo " 本月共有:".date("t")."天";   echo " 当前年份".date('Y');   
   echo " 当前月份".date('m');   
   echo " 当前几号".date('d');   
   echo "
";

4. Use functions and arrays to get the first and last days of the month and compare Practical

  function getthemonth($date){
       $firstday = date('Y-m-01', strtotime($date));       
       $lastday = date('Y-m-d', strtotime("$firstday +1 month -1 day"));       
       return array($firstday,$lastday);
  }   
  $today = date("Y-m-d");   
  $day=getthemonth($today);   
  echo "当月的第一天: ".$day[0]." 当月的最后一天: ".$day[1];   
  echo "
";

Get the date of this month:

function getMonth($date){
      $firstday = date("Y-m-01",strtotime($date));      
      $lastday = date("Y-m-d",strtotime("$firstday +1 month -1 day"));      
      return array($firstday,$lastday);
}

 $firstday is the first day of the month, if$date If is 2018-2, $firstday will be 2018-02-01, and then add one month based on $firstday to get 2018-03-01, minus one day is 2018-02-28, it is so convenient to use date() and strtotime().

Get the date of the previous month:

function getlastMonthDays($date){      
$timestamp=strtotime($date);      
$firstday=date('Y-m-01',strtotime(date('Y',$timestamp).'-'.(date('m',$timestamp)-1).'-01'));      
$lastday=date('Y-m-d',strtotime("$firstday +1 month -1 day"));      
return array($firstday,$lastday);
}

The date of the previous month needs to get a timestamp first, and then -1 on the month OK, the super smart date() will convert things like 2018-0-1 into 2017-12-01, so cool .

Get the date of the next month:

function getNextMonthDays($date){      
$timestamp=strtotime($date);      
$arr=getdate($timestamp);      
if($arr['mon'] == 12){            
$year=$arr['year'] +1;            
$month=$arr['mon'] -11;            
$firstday=$year.'-0'.$month.'-01';            
$lastday=date('Y-m-d',strtotime("$firstday +1 month -1 day"));
      }else{            
      $firstday=date('Y-m-01',strtotime(date('Y',$timestamp).'-'.(date('m',$timestamp)+1).'-01'));            
      $lastday=date('Y-m-d',strtotime("$firstday +1 month -1 day"));
      }     
       return array($firstday,$lastday);
}

The code for the date of the next month looks a little longer, because date() cannot be converted to something like 2018-13-01 This kind of thing will go back directly to 1970, so we need to deal with the issue of 12 months before, except 12 Just enter month 1 and it will be OK.

Generally speaking, the date function is too powerful.

Related recommendations:

Use PHP to obtain visitor IP, regional location, browser and source page information


The above is the detailed content of php gets some time to implement method practice. 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