


PHP gets the dates of last month, next month, and this month strtotime,date
When I was writing a program today, I suddenly discovered a function I wrote a long time ago to get the number of days in a month. It is a classic switch version. But when I got the number of days in the previous month, I just changed the month by -1. I guess I was too sleepy at the time. I saw a creepy feeling, and originally wanted to deal with it again, but then I thought there must be some super convenient method, so I found the version below and made some minor modifications.
Get the date of this month:
1 function getMonth($date){ 2 $firstday = date("Y-m-01",strtotime($date)); 3 $lastday = date("Y-m-d",strtotime("$firstday +1 month -1 day")); 4 return array($firstday,$lastday); 5 }
$firstday is the first day of the month. If $date is 2014-2, $firstday will be 2014-02-01, and then add one month based on $firstday. 2014-03-01, minus one day, is 2014-02-28. It is so convenient to use date() and strtotime().
Get the date of last month:
1 function getlastMonthDays($date){ 2 $timestamp=strtotime($date); 3 $firstday=date('Y-m-01',strtotime(date('Y',$timestamp).'-'.(date('m',$timestamp)-1).'-01')); 4 $lastday=date('Y-m-d',strtotime("$firstday +1 month -1 day")); 5 return array($firstday,$lastday); 6 }
The date of last month needs to get a timestamp first, and then add -1 to the month. Then the super smart date() will convert things like 2014-0-1 into 2013-12-01, so cool.
Get next month’s date:
1 function getNextMonthDays($date){ 2 $timestamp=strtotime($date); 3 $arr=getdate($timestamp); 4 if($arr['mon'] == 12){ 5 $year=$arr['year'] +1; 6 $m -11; 7 $firstday=$year.'-0'.$month.'-01'; 8 $lastday=date('Y-m-d',strtotime("$firstday +1 month -1 day")); 9 }else{ 10 $firstday=date('Y-m-01',strtotime(date('Y',$timestamp).'-'.(date('m',$timestamp)+1).'-01')); 11 $lastday=date('Y-m-d',strtotime("$firstday +1 month -1 day")); 12 } 13 return array($firstday,$lastday); 14 }
The code for next month’s date looks a little longer, because date() cannot convert something like 2014-13-01, it It will go back to 1970 directly, so we need to deal with the issue of December first. Except for December, just add the month + 1 and it will be OK.
In general, it is very convenient, the date function is too powerful.
The above introduces PHP to get the dates strtotime, date of last month, next month, and this month, including the content. I hope it will be helpful to friends who are interested in PHP tutorials.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

If you're looking for a way to automatically create and name files and folders based on system timestamps, you've come to the right place. There is a super simple way to accomplish this task. The created folders or files can then be used for various purposes such as storing file backups, sorting files based on date, etc. In this article, we will explain in some very simple steps how to automatically create files and folders in Windows 11/10 and name them according to the system’s timestamp. The method used is a batch script, which is very simple. Hope you enjoyed reading this article. Section 1: How to automatically create and name a folder based on the current timestamp of the system Step 1: First, navigate to the parent folder where you want to create the folder,

When developing using PHP programs, you often encounter some warning or error messages. Among them, one error message that may appear is: PHPWarning:date()expectsparameter2tobelong,stringgiven. The error message means: the second parameter of the function date() is expected to be a long integer (long), but what is actually passed to it is a string (string). So, we

The timestamp refers to the total number of seconds from 00:00:00 on January 1, 1970, Greenwich Mean Time (08:00:00 on January 1, 1970, Beijing time) to the present. Various versions of MySQL are deployed in the production environment, including three major versions of MySQL 5.5/5.6/5.7 and N minor versions. Due to the poor upward compatibility of MySQL, the same SQL behaves differently in different versions. As follows The timestamp data type is introduced in detail from several aspects. Timestamp data access In the above three major versions of MySQL, the value range of the default timestamp (Timestamp) type is’1970-01-0100:00:01&r

1. Introduction The Date class in the java.util package represents a specific time, accurate to milliseconds. If we want to use our Date class, then we must introduce our Date class. Writing the year directly into the Date class will not produce the correct result. Because Date in Java is calculated from 1900, so as long as you fill in the first parameter with the number of years since 1900, you will get the year you want. The month needs to be subtracted by 1, and the day can be inserted directly. This method is rarely used, and the second method is commonly used. This method is to convert a string that conforms to a specific format, such as yyyy-MM-dd, into Date type data. First, define an object of Date type Date

There are many excellent calendar libraries and date libraries in Python for us to use. These libraries can help us handle date and calendar related operations. Next, I will introduce you to several common choices and provide corresponding code examples. Datetime library: Datetime is Python's built-in date and time processing module. It provides many date and time related classes and methods, which can be used to process dates, times, time differences and other operations. Sample code: importdatetime#Get the current date

How to get millisecond representation of date using getTime() method of Date class In Java, Date class is a class used to represent date and time. It provides many useful methods to manipulate and obtain information about date objects. Among them, the getTime() method is an important method in the Date class, which can return the millisecond representation of the date object. Next, we will detail how to use this method to obtain the millisecond representation of a date, and provide corresponding code examples. Using the Date class

I encountered a problem. After springboot was upgraded to 2.0, the date found from the database was received using Date and finally returned directly to the front end. It can be displayed normally in the yyyy-MM-ddHH:mm:ss format in Google Chrome. However, the date displayed in the IE browser is "garbled" because the springboot1.x version returns a timestamp in the Date field by default, and Google and IE will automatically convert the timestamp into yyyy-MM-ddHH:mm:ss ;After springboot2.0, spring will automatically convert the Date field into a UTC string (without configuration), so date needs to be converted into a timestamp or y

Stringbuild class Since the object content of the String class cannot be changed, a new String object will be constructed every time it is spliced, which is time-consuming and wastes memory space. At this time, you need to solve this problem through the StringBuild class provided by Java. StringBuilder is also called a variable character sequence. , it is a string buffer similar to String, which can be regarded as a container. Many strings can be held in the container. Variable means that the content in the StringBuilder object is variable. The construction method publicStringBuilder(): creates an empty buffer publicStringBuilder(Stringsr
