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 Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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)

11 Best PHP URL Shortener Scripts (Free and Premium) 11 Best PHP URL Shortener Scripts (Free and Premium) Mar 03, 2025 am 10:49 AM

Long URLs, often cluttered with keywords and tracking parameters, can deter visitors. A URL shortening script offers a solution, creating concise links ideal for social media and other platforms. These scripts are valuable for individual websites a

Introduction to the Instagram API Introduction to the Instagram API Mar 02, 2025 am 09:32 AM

Following its high-profile acquisition by Facebook in 2012, Instagram adopted two sets of APIs for third-party use. These are the Instagram Graph API and the Instagram Basic Display API.As a developer building an app that requires information from a

Working with Flash Session Data in Laravel Working with Flash Session Data in Laravel Mar 12, 2025 pm 05:08 PM

Laravel simplifies handling temporary session data using its intuitive flash methods. This is perfect for displaying brief messages, alerts, or notifications within your application. Data persists only for the subsequent request by default: $request-

Build a React App With a Laravel Back End: Part 2, React Build a React App With a Laravel Back End: Part 2, React Mar 04, 2025 am 09:33 AM

This is the second and final part of the series on building a React application with a Laravel back-end. In the first part of the series, we created a RESTful API using Laravel for a basic product-listing application. In this tutorial, we will be dev

Simplified HTTP Response Mocking in Laravel Tests Simplified HTTP Response Mocking in Laravel Tests Mar 12, 2025 pm 05:09 PM

Laravel provides concise HTTP response simulation syntax, simplifying HTTP interaction testing. This approach significantly reduces code redundancy while making your test simulation more intuitive. The basic implementation provides a variety of response type shortcuts: use Illuminate\Support\Facades\Http; Http::fake([ 'google.com' => 'Hello World', 'github.com' => ['foo' => 'bar'], 'forge.laravel.com' =>

cURL in PHP: How to Use the PHP cURL Extension in REST APIs cURL in PHP: How to Use the PHP cURL Extension in REST APIs Mar 14, 2025 am 11:42 AM

The PHP Client URL (cURL) extension is a powerful tool for developers, enabling seamless interaction with remote servers and REST APIs. By leveraging libcurl, a well-respected multi-protocol file transfer library, PHP cURL facilitates efficient execution of various network protocols, including HTTP, HTTPS, and FTP. This extension offers granular control over HTTP requests, supports multiple concurrent operations, and provides built-in security features.

12 Best PHP Chat Scripts on CodeCanyon 12 Best PHP Chat Scripts on CodeCanyon Mar 13, 2025 pm 12:08 PM

Do you want to provide real-time, instant solutions to your customers' most pressing problems? Live chat lets you have real-time conversations with customers and resolve their problems instantly. It allows you to provide faster service to your custom

Announcement of 2025 PHP Situation Survey Announcement of 2025 PHP Situation Survey Mar 03, 2025 pm 04:20 PM

The 2025 PHP Landscape Survey investigates current PHP development trends. It explores framework usage, deployment methods, and challenges, aiming to provide insights for developers and businesses. The survey anticipates growth in modern PHP versio

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

……