Exercise caution when using php's strtotime () function, phpstrtotime Function

Source: Internet
Author: User

Exercise caution when using php's strtotime () function, phpstrtotime Function

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

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 <? Php 2 3 $ testTime = date ("Y-m-d H: I: s", time (); 4 echo "test time: {$ testTime} \ r \ n "; 5 6 $ flag = 0; 7 $ time = 0; 8 $ tmp = 0; 9 10 while (1) 11 {12 if ($ flag ++> 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:

<? Php
Echo date ("Y-m-d", strtotime ("+ 2 days "));
Echo '<br/> ';
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 );

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.