Home > Backend Development > PHP Tutorial > PHP method to obtain the timestamps of the current month and the beginning and end of the previous month

PHP method to obtain the timestamps of the current month and the beginning and end of the previous month

墨辰丷
Release: 2023-03-28 12:00:01
Original
4269 people have browsed it

This article mainly introduces the method of PHP to obtain the timestamps of the current month and the beginning and end of the previous month. It involves PHP's judgment and operation skills related to date and time. Friends in need can refer to the following

for details As follows:

Current month

<?php
$thismonth = date(&#39;m&#39;);
$thisyear = date(&#39;Y&#39;);
$startDay = $thisyear . &#39;-&#39; . $thismonth . &#39;-1&#39;;
$endDay = $thisyear . &#39;-&#39; . $thismonth . &#39;-&#39; . date(&#39;t&#39;, strtotime($startDay));
$b_time  = strtotime($startDay);//当前月的月初时间戳
$e_time  = strtotime($endDay);//当前月的月末时间戳
Copy after login

Previous month

<?php
$thismonth = date(&#39;m&#39;);
$thisyear = date(&#39;Y&#39;);
if ($thismonth == 1) {
 $lastmonth = 12;
 $lastyear = $thisyear - 1;
} else {
 $lastmonth = $thismonth - 1;
 $lastyear = $thisyear;
}
$lastStartDay = $lastyear . &#39;-&#39; . $lastmonth . &#39;-1&#39;;
$lastEndDay = $lastyear . &#39;-&#39; . $lastmonth . &#39;-&#39; . date(&#39;t&#39;, strtotime($lastStartDay));
$b_time = strtotime($lastStartDay);//上个月的月初时间戳
$e_time = strtotime($lastEndDay);//上个月的月末时间戳
Copy after login

The key here is t in the date function, which is used to obtain the number of days in the current month, 28 days, 29 days, 30 days, and 31 days. The number of days it contains is the number at the end of the month.

The above is the entire content of this article, I hope it will be helpful to everyone's study.


Related recommendations:

How to insert multiple pieces of data through PHP MySQL

Php multi-process implementation programming example

How to insert data through PHP MySQL

The above is the detailed content of PHP method to obtain the timestamps of the current month and the beginning and end of the previous month. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
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