Exercise caution when using php's strtotime () function, phpstrtotime function. Exercise caution when using the strtotime () function of php. in our daily business, we often use time-based horizontal table sharding for databases for business volumes, the strtotime () function and phpstrtotime function of php are usually used for queries after table sharding.
In our daily business, we often use time-based horizontal table sharding for the business volume. queries after table sharding often involve time issues. For example, we want to query the order of a user one month from the current time. at this time, some of us will use the strtotime () function for processing.
However, you must be cautious when using strtotime. Let's take a look at the code to get the month of the year a few months ago. for example, today is July 22, August 1, 2014, the month of the year I want to get 2 months ago is array ("0" => "201406", "1" => "201407 ",)
1/***** 2*$3 * return like array ('0' => '000000', '1' => '000000') a few months ago '), the result does not contain the current month 4 (); 8 9 if ($ mthNum <= 0) 10 return $ timeArr; 11 12 do 13 {14 $ timeArr [] = date ("Ym ", strtotime ("-$ mthNum month"); 15 $ mthNum --; 16} 17 while ($ mthNum> 0); 18 19 return $ timeArr; 20}
The code seems to be okay on the surface, but let's do a test. below is the test code. the purpose of the test is very simple. I just want to test the date of the previous month on the last day of each month.
1
Let's take a look at the test results. from the test results, we found that we ignoredDifferent days per month, Strtotime () will bring different results
So how exactly is strtotime ("-$ n month") calculated? Perform a test as follows:
1
12) 13 break; 14 15 $ time = strtotime ("-$ flag month"); 16 $ monthDiff = ($ time-$ tmp)/86400; // 86400 = 24*60*60, 17 $ tmp = $ time; 18 19 $ dispDate = date ("Y-m-d H: I: s", $ time ); 20 21 echo "{$ flag} months ago: {$ time}, date: {$ dispDate)} difference: {$ dispDate} day \ r \ n"; 22}
(Note: strtotime ("-$ n month"), the second parameter is omitted, the second parameter indicates the distance time, and the omitted parameter indicates the current time)
Time |
Difference value |
Theoretical time |
Result |
July 31 |
Before January 1, January |
June 31 |
January 1, June is only 30 days, plus one day to January 1, July 1. |
July 31 |
Before January 1, February |
May 31 |
|
July 31 |
Before January 1, March |
April 31 |
January 1, April is only 30 days, plus one day to January 1, May 1. |
...... |
So how can we use strtotime ("-$ n month") to process our needs?
The following is a handwritten code for your reference.
1/***************** 2 solve the problem of the year and month between the two time periods 3 * $ btm, $ EZ is a unix timestamp 4 ******************/5 function getTimeDis ($ btm, $ ez) 6 {7 $ resArr = array (); 8 if ($ b7-< $ btm) 9 return $ resArr; 10 11 // Convert both btm and EZ to 12 $ btmc = strtotime (date ("Y-m-01 00:00:00", $ btm) each month )); 13 $ etmc = strtotime (date ("Y-m-01 00:00:00", $ etm); 14 15 16 $ flag = 0; // Time difference identifier 17 $ resArr [] = date ("Ym", $ etmc); 18 19 while (1) 20 {21 $ flag ++; 22 $ compTime = strtotime ("-{$ flag} month", $ etmc); 23 24 if ($ compTime <$ btm) 25 break; 26 27 $ resArr [] = date ("Ym", $ compTime); 28} 29 30 return array_unique ($ resArr); 31}
Strtotime function in php
Mktime () must contain parameters.
The simplest way to get two days 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 description. you can also
+ 10 hours
-2 hours
And so on.
How does the php strtotime function change at the specified time?
$ Date = date ("Y-m-d", strtotime ('2017-01-20 ') + 60*60*24 );
Sort () function, phpstrtotime function in our daily business, for the business volume, we often use horizontal table sharding for the database by time, after the table sharding query will often...