Table of Contents
Use php's strtotime() function carefully, phpstrtotime function
June 31
So if this is the case, how do we use strtotime("-$n month") to handle our needs?
Home Backend Development PHP Tutorial Use php's strtotime() function with caution, phpstrtotime function_PHP tutorial

Use php's strtotime() function with caution, phpstrtotime function_PHP tutorial

Jul 13, 2016 am 10:21 AM
strtotime

Use php's strtotime() function carefully, phpstrtotime function

In our daily business, in view of the business volume, we often use the horizontal partitioning of the database by time. Later inquiries often involve time issues. For example, we want to query the order status of a user one month from the current time. At this time, we will use the strtotime() function to process.

But when using strtotime(), you need to be very careful. Let's look at a piece of code first. The purpose of the code is to get the year and month a few months ago. For example, today is August 1, 2014. I want to get the year and month 2 months ago, which is array("0"=> "201406", "1"=>"201407",)

<span> 1</span> <span>/*</span><span>***
</span><span> 2</span> <span>*$mthNum 几月以前
</span><span> 3</span> <span>* return like array('0'=>'201401','1'=>'201402'),结果不包含当前月份
</span><span> 4</span> <span>***********</span><span>*/</span>
<span> 5</span> <span>function</span> getTimeYm(<span>$mthNum</span><span>)
</span><span> 6</span> <span>{
</span><span> 7</span>     <span>$timeArr</span> = <span>array</span><span>();
</span><span> 8</span>     
<span> 9</span>     <span>if</span>(<span>$mthNum</span> <= 0<span>)
</span><span>10</span>         <span>return</span> <span>$timeArr</span><span>;
</span><span>11</span> 
<span>12</span>     <span>do</span> 
<span>13</span> <span>    {
</span><span>14</span>         <span>$timeArr</span>[] = <span>date</span>("Ym", <span>strtotime</span>("-<span>$mthNum</span> month"<span>));
</span><span>15</span>         <span>$mthNum</span> --<span>;
</span><span>16</span> <span>    }
</span><span>17</span>     <span>while</span> (<span>$mthNum</span> > 0<span>);
</span><span>18</span> 
<span>19</span>     <span>return</span> <span>$timeArr</span><span>;
</span><span>20</span> }
Copy after login

 

On the surface, there seems to be no problem with the code, but let’s do a test. The following is the test code. The purpose of the test is very simple. I just want to test what is the date of the month before the last day of each month

<span> 1</span> <?<span>php
</span><span> 2</span> <span>$dateArr</span> = <span>array</span><span>(
</span><span> 3</span>     "2014-01-31    00:00:00 -1 month",
<span> 4</span>     "2014-02-28    00:00:00 -1 month",
<span> 5</span>     "2014-03-31    00:00:00 -1 month",
<span> 6</span>     "2014-04-30    00:00:00 -1 month",
<span> 7</span>     "2014-05-31    00:00:00 -1 month",
<span> 8</span>     "2014-06-30    00:00:00 -1 month",
<span> 9</span>     "2014-07-31    00:00:00 -1 month",
<span>10</span>     "2014-08-31    00:00:00 -1 month",
<span>11</span>     "2014-09-30    00:00:00 -1 month",
<span>12</span>     "2014-10-31    00:00:00 -1 month",
<span>13</span>     "2014-11-30    00:00:00 -1 month",
<span>14</span>     "2014-12-31    00:00:00 -1 month",
<span>15</span> <span>);
</span><span>16</span> 
<span>17</span> <span>foreach</span> (<span>$dateArr</span> <span>as</span> <span>$val</span><span>)
</span><span>18</span> <span>{
</span><span>19</span>     <span>$time</span> = <span>strtotime</span>(<span>$val</span><span>);
</span><span>20</span>     <span>echo</span> [<span>$time</span>][<span>$val</span>]."\r\n"<span>;
</span><span>21</span> }  
Copy after login

Let’s take a look at the test results. From the test results, we found that we ignored the number of days in each month is different, then strtotime() will bring different results

So how is strtotime("-$n month") calculated? Doing a test, as follows: Check the results

<span> 1</span> <?<span>php
</span><span> 2</span> 
<span> 3</span> <span>$testTime</span> = <span>date</span>("Y-m-d H:i:s", <span>time</span><span>());
</span><span> 4</span> <span>echo</span> "测试时间:{<span>$testTime</span>}  \r\n"<span>;
</span><span> 5</span> 
<span> 6</span> <span>$flag</span> = 0<span>; 
</span><span> 7</span> <span>$time</span> = 0<span>;
</span><span> 8</span> <span>$tmp</span>  = 0<span>;
</span><span> 9</span> 
<span>10</span> <span>while</span>(1<span>)
</span><span>11</span> <span>{
</span><span>12</span>     <span>if</span>(<span>$flag</span> ++ > 12<span>)
</span><span>13</span>         <span>break</span><span>;
</span><span>14</span> 
<span>15</span>     <span>$time</span>      =  <span>strtotime</span>("-<span>$flag</span> month"<span>);
</span><span>16</span>     <span>$monthDiff</span> = (<span>$time</span> - <span>$tmp</span>)/86400;  <span>//</span><span>86400 = 24 * 60 * 60,</span>
<span>17</span>     <span>$tmp</span>       = <span>$time</span><span>;
</span><span>18</span> 
<span>19</span>     <span>$dispDate</span> = <span>date</span>("Y-m-d H:i:s", <span>$time</span><span>);
</span><span>20</span> 
<span>21</span>     <span>echo</span> "{<span>$flag</span>}月前: {<span>$time</span>},  日期:{<span>$dispDate</span>)}   差值:{<span>$dispDate</span>}天 \r\n"<span>;
</span><span>22</span> }
Copy after login

Use php's strtotime() function with caution, phpstrtotime function_PHP tutorial(Note: strtotime("-$n month"), the second parameter is omitted, the second parameter indicates the distance time, omitting indicates the current time)

So if this is the case, how do we use strtotime("-$n month") to handle our needs?

A piece of handwritten code is provided below for reference


Problem with strtotime function in php

mktime() requires parameters. The easiest way to get two days later is: echo date("Y-m-d",strtotime("+2 days")); echo '
';
echo date("Y-m-d H:i:s",strtotime("+2 days"));
?>Take a good look at the strtotime function Note, you can also +10 hours
-2 hours
and so on. How does the php strtotime function change the specified time? $date = date("Y-m-d", strtotime('2012-01-20') + 60*60*24); http://www.bkjia.com/PHPjc/855628.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/855628.htmlTechArticleUse php’s strtotime() function with caution. The phpstrtotime function is often used in our daily business based on business volume. The database is divided into tables horizontally according to time. The query after the table is divided will often...
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Describe the SOLID principles and how they apply to PHP development. Describe the SOLID principles and how they apply to PHP development. Apr 03, 2025 am 12:04 AM

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to automatically set permissions of unixsocket after system restart? How to automatically set permissions of unixsocket after system restart? Mar 31, 2025 pm 11:54 PM

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

Explain the concept of late static binding in PHP. Explain the concept of late static binding in PHP. Mar 21, 2025 pm 01:33 PM

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

How to send a POST request containing JSON data using PHP's cURL library? How to send a POST request containing JSON data using PHP's cURL library? Apr 01, 2025 pm 03:12 PM

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...

How to debug CLI mode in PHPStorm? How to debug CLI mode in PHPStorm? Apr 01, 2025 pm 02:57 PM

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

Framework Security Features: Protecting against vulnerabilities. Framework Security Features: Protecting against vulnerabilities. Mar 28, 2025 pm 05:11 PM

Article discusses essential security features in frameworks to protect against vulnerabilities, including input validation, authentication, and regular updates.

See all articles

  时间

差值

理论时间

结果

7月31号

1月前

6月31号

6月只有30天,则加一天到7月1号

7月31号

2月前

5月31号

7月31号

3月前

4月31号

4月只有30天,则加一天到5月1号

……

Time

Difference

Theory time

<span> 1</span> <span>/*</span><span>***************
</span><span> 2</span> <span>*解决两个时间段之间的年月
</span><span> 3</span> <span>* $btm, $etm 是unix时间戳
</span><span> 4</span> <span>****************</span><span>*/</span>
<span> 5</span> <span>function</span> getTimeDis(<span>$btm</span>, <span>$etm</span><span>)
</span><span> 6</span> <span>{
</span><span> 7</span>     <span>$resArr</span> = <span>array</span><span>();
</span><span> 8</span>     <span>if</span>(<span>$etm</span> < <span>$btm</span><span>)
</span><span> 9</span>         <span>return</span> <span>$resArr</span><span>;
</span><span>10</span> 
<span>11</span>     <span>//</span><span>将btm和etm都转成每月1号</span>
<span>12</span>     <span>$btmc</span> = <span>strtotime</span>(<span>date</span>("Y-m-01 00:00:00", <span>$btm</span><span>));
</span><span>13</span>     <span>$etmc</span> = <span>strtotime</span>(<span>date</span>("Y-m-01 00:00:00", <span>$etm</span><span>));
</span><span>14</span> 
<span>15</span> 
<span>16</span>     <span>$flag</span> = 0; <span>//</span><span>时间差标识符</span>
<span>17</span>     <span>$resArr</span>[] = <span>date</span>("Ym", <span>$etmc</span><span>);
</span><span>18</span> 
<span>19</span>     <span>while</span>(1<span>)
</span><span>20</span> <span>    {
</span><span>21</span>         <span>$flag</span> ++<span>;
</span><span>22</span>         <span>$compTime</span> = <span>strtotime</span>("-{<span>$flag</span>} month",  <span>$etmc</span><span>);
</span><span>23</span>         
<span>24</span>         <span>if</span>(<span>$compTime</span> < <span>$btm</span><span>)
</span><span>25</span>             <span>break</span><span>;
</span><span>26</span> 
<span>27</span>         <span>$resArr</span>[] = <span>date</span>("Ym", <span>$compTime</span><span>);
</span><span>28</span> <span>    }
</span><span>29</span> 
<span>30</span>     <span>return</span> <span>array_unique</span>(<span>$resArr</span><span>);
</span><span>31</span> }
Copy after login
Results

July 31
1 month ago

June 31

There are only 30 days in June, so add one day to July 1
July 31

2 months ago


May 31



July 31

3 months ago

April 31

April only has 30 days, so add one day to May 1

……