


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.

如果您正在寻找根据系统时间戳自动创建文件和文件夹并为其命名的方法,那么您来对地方了。有一种超级简单的方法可以用来完成这项任务。然后,创建的文件夹或文件可用于各种目的,例如存储文件备份、根据日期对文件进行排序等。在本文中,我们将通过一些非常简单的步骤解释如何在Windows11/10中自动创建文件和文件夹,并根据系统的时间戳对其进行命名。使用的方法是批处理脚本,非常简单。希望你喜欢阅读这篇文章。第1节:如何根据系统当前时间戳自动创建文件夹并命名第1步:首先,导航到要在其中创建文件夹的父文件夹,

在使用PHP程序开发时,经常会碰到一些警告或者错误的提示信息。其中,可能出现的一个错误提示就是:PHPWarning:date()expectsparameter2tobelong,stringgiven。这个错误的提示信息意思是:函数date()的第二个参数期望是长整型(long),但是实际传递给它的是字符串(string)。那么,我们

时间戳是指格林威治时间1970年01月01日00时00分00秒(北京时间1970年01月01日08时00分00秒)起至现在的总秒数。生产环境中部署着各种版本的MySQL,包括MySQL5.5/5.6/5.7三个大版本和N个小版本,由于MySQL在向上兼容性较差,导致相同SQL在不同版本上表现各异,下面从几个方面来详细介绍时间戳数据类型。时间戳数据存取在MySQL上述三个大版本中,默认时间戳(Timestamp)类型的取值范围为’1970-01-0100:00:01&r

一.介绍java.util包中的Date类表示特定的时间,精确到毫秒。如果要想使用我们的Date类,那么我们必须得引入我们的Date类。Date类直接写入年份是得不到正确的结果的。因为java中Date是从1900年开始算的,所以前面的第一个参数只要填入从1900年后过了多少年就是你想要得到的年份。月需要减1,日可以直接插入。这种方法用的比较少,常用的是第二种方法。这种方法是将一个符合特定格式,比如yyyy-MM-dd,的字符串转化成为Date类型的数据。首先,定义一个Date类型的对象Date

如何使用Date类的getTime()方法获取日期的毫秒表示形式在Java中,Date类是用于表示日期和时间的类。它提供了许多有用的方法来操作和获取日期对象的信息。其中,getTime()方法是Date类中的一个重要方法,它可以返回日期对象的毫秒表示形式。接下来,我们将详细介绍如何使用这个方法来获取日期的毫秒表示形式,并提供相应的代码示例。使用Date类的g

Python中有许多优秀的日历库和日期库供我们使用,这些库可以帮助我们处理日期和日历相关的操作。接下来,我将为大家介绍几个常用的选择,并提供相应的代码示例。datetime库:datetime是Python内置的日期和时间处理模块,提供了许多日期和时间相关的类和方法,可以用于处理日期、时间、时间差等操作。示例代码:importdatetime#获取当

Stringbuild类由于String类的对象内容不可改变,每次拼接都会构建一个新的String对象,既耗时,又浪费内存空间这时需要通过java提供的StringBuild类解决这个问题StringBuilder又称为可变字符序列,它是一个类似于String的字符串缓冲区,可以看作是一个容器,容器中可以装很多字符串可变指的是StringBuilder对象中的内容是可变的构造方法publicStringBuilder():创建一个空的缓冲区publicStringBuilder(Stringsr

遇到一个问题,springboot升级成2.0后,从数据库查出来的日期,用Date接收,最后直接返回给前端,在谷歌浏览器中能正常显示成yyyy-MM-ddHH:mm:ss格式。但是在IE浏览器中日期显示的是“乱码”,因为springboot1.x版本的默认将Date字段返回的是时间戳,而谷歌、IE都会自动将时间戳转换成yyyy-MM-ddHH:mm:ss;在springboot2.0后,spring会将Date字段自动给转成UTC字符串了(在没有配置的情况下),所以date需要转换成时间戳还是y


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

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

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

Atom editor mac version download
The most popular open source editor

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.
