php gets some time to implement method practice

不言
Release: 2023-03-23 17:36:02
Original
1336 people have browsed it

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 "<br/>";   
   echo date(&#39;Y-m-t&#39;, strtotime(&#39;-1 month&#39;));   
   echo "<br/>";
Copy after login
Copy after login

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

Previous The first day of the month:
 

echo date(&#39;Y-m-d&#39;, strtotime(date(&#39;Y-m-01&#39;) . &#39; -1 month&#39;));
// 计算出本月第一天再减一个月
Copy after login

The last day of the previous month:
 

echo date(&#39;Y-m-d&#39;, strtotime(date(&#39;Y-m-01&#39;) . &#39; -1 day&#39;)); 
// 计算出本月第一天再减一天
Copy after login

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

   $BeginDate=date(&#39;Y-m-01&#39;, strtotime(date("Y-m-d")));   
   echo $BeginDate;   echo "<br/>";   
   echo date(&#39;Y-m-d&#39;, strtotime("$BeginDate +1 month -1 day"));   
   echo "<br/>";
Copy after login
Copy after login

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

   echo " 本月共有:".date("t")."天";   
   echo " 当前年份".date(&#39;Y&#39;);   
   echo " 当前月份".date(&#39;m&#39;);   
   echo " 当前几号".date(&#39;d&#39;);   
   echo "<br/>";
Copy after login

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

  function getthemonth($date){
       $firstday = date(&#39;Y-m-01&#39;, strtotime($date));       
       $lastday = date(&#39;Y-m-d&#39;, strtotime("$firstday +1 month -1 day"));       
       return array($firstday,$lastday);
  }   
  $today = date("Y-m-d");   
  $day=getthemonth($today);   
  echo "当月的第一天: ".$day[0]." 当月的最后一天: ".$day[1];  
  echo "<br/>";
Copy after login

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);
}
Copy after login
Copy after login

 $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(&#39;Y-m-01&#39;,strtotime(date(&#39;Y&#39;,$timestamp).&#39;-&#39;.(date(&#39;m&#39;,$timestamp)-1).&#39;-01&#39;));      
$lastday=date(&#39;Y-m-d&#39;,strtotime("$firstday +1 month -1 day"));      return array($firstday,$lastday);
}
Copy after login

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[&#39;mon&#39;] == 12){            
$year=$arr[&#39;year&#39;] +1;            
$month=$arr[&#39;mon&#39;] -11;            
$firstday=$year.&#39;-0&#39;.$month.&#39;-01&#39;;            
$lastday=date(&#39;Y-m-d&#39;,strtotime("$firstday +1 month -1 day"));
      }else{            
      $firstday=date(&#39;Y-m-01&#39;,strtotime(date(&#39;Y&#39;,$timestamp).&#39;-&#39;.(date(&#39;m&#39;,$timestamp)+1).&#39;-01&#39;));            
      $lastday=date(&#39;Y-m-d&#39;,strtotime("$firstday +1 month -1 day"));
      }      
      return array($firstday,$lastday);
}
Copy after login

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(&#39;Y-m-01&#39;, strtotime(&#39;-1 month&#39;));   
   echo "<br/>";   
   echo date(&#39;Y-m-t&#39;, strtotime(&#39;-1 month&#39;));   
   echo "<br/>";
Copy after login
Copy after login

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

Previous The first day of the month:
 

echo date(&#39;Y-m-d&#39;, strtotime(date(&#39;Y-m-01&#39;) . &#39; -1 month&#39;));// 计算出本月第一天再减一个月
Copy after login

The last day of the previous month:
 

echo date(&#39;Y-m-d&#39;, strtotime(date(&#39;Y-m-01&#39;) . &#39; -1 day&#39;)); 
// 计算出本月第一天再减一天
Copy after login

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

   $BeginDate=date(&#39;Y-m-01&#39;, strtotime(date("Y-m-d")));   
   echo $BeginDate;   echo "<br/>";   
   echo date(&#39;Y-m-d&#39;, strtotime("$BeginDate +1 month -1 day"));   
   echo "<br/>";
Copy after login
Copy after login

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

   echo " 本月共有:".date("t")."天";   echo " 当前年份".date(&#39;Y&#39;);   
   echo " 当前月份".date(&#39;m&#39;);   
   echo " 当前几号".date(&#39;d&#39;);   
   echo "<br/>";
Copy after login

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

  function getthemonth($date){
       $firstday = date(&#39;Y-m-01&#39;, strtotime($date));       
       $lastday = date(&#39;Y-m-d&#39;, strtotime("$firstday +1 month -1 day"));       
       return array($firstday,$lastday);
  }   
  $today = date("Y-m-d");   
  $day=getthemonth($today);   
  echo "当月的第一天: ".$day[0]." 当月的最后一天: ".$day[1];   
  echo "<br/>";
Copy after login

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);
}
Copy after login
Copy after login

 $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(&#39;Y-m-01&#39;,strtotime(date(&#39;Y&#39;,$timestamp).&#39;-&#39;.(date(&#39;m&#39;,$timestamp)-1).&#39;-01&#39;));      
$lastday=date(&#39;Y-m-d&#39;,strtotime("$firstday +1 month -1 day"));      
return array($firstday,$lastday);
}
Copy after login

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[&#39;mon&#39;] == 12){            
$year=$arr[&#39;year&#39;] +1;            
$month=$arr[&#39;mon&#39;] -11;            
$firstday=$year.&#39;-0&#39;.$month.&#39;-01&#39;;            
$lastday=date(&#39;Y-m-d&#39;,strtotime("$firstday +1 month -1 day"));
      }else{            
      $firstday=date(&#39;Y-m-01&#39;,strtotime(date(&#39;Y&#39;,$timestamp).&#39;-&#39;.(date(&#39;m&#39;,$timestamp)+1).&#39;-01&#39;));            
      $lastday=date(&#39;Y-m-d&#39;,strtotime("$firstday +1 month -1 day"));
      }     
       return array($firstday,$lastday);
}
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!