PHP中使用strtotime函数注意事项
strtotime函数在是大家常用的一个时间日期转换成单位S的一个函数了,但是这个函数有一个细节大家可能没有注意到,就是 使用的时候需要考虑 有31天和 2月份的情况,所以,如果没有考虑这个特殊的情况,那么在每月的31号或者和2月份有关的时间点时候就可能出现开始的那个灵异情况了,这个情况很多朋友都不记得了,但在很多时间这个非常重要,下面举例子来给各位介绍一下。
今天在微博上看到一个小案例,命令行如下:
代码如下 | 复制代码 |
php -r “echo date(‘Y/m’,strtotime(‘-2 months’)) . \”\n\”;” |
输出结果如下:
2013/03
如果把命令行修改成如下:
代码如下 | 复制代码 |
php -r “echo date(‘Y/m’,strtotime(‘-3 months’)) . \”\n\”;” |
输出结果如下:
2013/03
这里就奇怪了,怎么两个月前和三个月前的年月份都是一样的,再看看下面的命令行:
代码如下 | 复制代码 |
php -r “echo date(‘Y/m’,strtotime(‘-3 months’,strtotime(’2013/05/28′))) . \”\n\”;” |
输出结果如下:
2013/02
如果修改命令行为如下:
代码如下 | 复制代码 |
php -r “echo date(‘Y/m’,strtotime(‘-3 months’,strtotime(’2013/05/29′))) . \”\n\”;” |
输出结果如下:
2013/03
下面这个与预期的结果是一样的,现在说说为什么会出现开始的那个奇怪的坑。
strtotime里的months和 month是30天,使用的时候需要考虑 有31天和 2月份的情况,所以,如果没有考虑这个特殊的情况,那么在每月的31号或者和2月份有关的时间点时候就可能出现开始的那个灵异情况,这个算是strtotime的一个坑吧,为了慎重起见,能不用的时候就尽量别去用这个坑人的东东。
PS:以上所有例子都是在Debian6的64位系统下执行,使用命令行方式执行,执行时间是2013-05-30.
上月下月时间不准确
以下有几种方法,可以帮助我们达到预期效果,比如我要返回上个月的月份:
代码如下 | 复制代码 |
echo date('M Y', strtotime('midnight first day of -1 month')); 或者: echo date('M Y', strtotime(date('Y-m-01')) - 86400); |
下方是其他的用途:
代码如下 | 复制代码 |
strtotime('first day of last month');
date_default_timezone_set('Asia/Shanghai'); date('Y年m月',$t), |

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

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

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

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

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

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

This article will explain in detail about formatting a local time/date in PHP. The editor thinks it is quite practical, so I share it with you as a reference. I hope you can gain something after reading this article. PHP Formatting Local Time/Date Formatting local time and date is a common task in PHP and can be achieved through PHP's built-in functions and classes. Built-in functions PHP provides several built-in functions to format time and date: date(): used to format the current time and date and return the result according to the provided format string. strftime(): Similar to date(), but it uses the POSIX strftime() function to provide more advanced formatting options. format parameter date
