Use PHP's Strtotime () function cautiously, phpstrtotime function _php Tutorial

Source: Internet
Author: User
Tags echo date

Use PHP's Strtotime () function sparingly, phpstrtotime function


We in the daily business, for the volume of business, often using the database by time to do horizontal sub-table, the query after the table will often involve time issues. For example, we would like to query a user's order for 1 months from the current time, at which point some of us would use the Strtotime () function to handle it.

But using Strtotime () requires a lot of caution. Let's look at a piece of code, the code is intended to get months of the year, for example, today is August 1, 2014, I want to get 2 months ago the year month is an array ("0" = "201406", "1" and "201407",)

1 /****2 * $mthNum a few months ago3 * return like array (' 0 ' = ' 201401 ', ' 1 ' = ' 201402 '), the result does not contain the current month4 ************/5 functionGettimeym ($mthNum)6 {7     $TIMEARR=Array();8     9     if($mthNum<= 0)Ten         return $TIMEARR; One  A      Do  -     { -         $TIMEARR[] =Date("Ym",Strtotime("-$mthNumMonth)); the         $mthNum--; -     } -      while($mthNum> 0); -  +     return $TIMEARR; -}

  

The surface look at the code seems to be no problem, but we do a test, the following is the test code, the purpose of the test is very simple, just want to test, the last day of the month the first one months of the date is how much

1 
 Php2 $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",Ten"2014-08-31 00:00:00-1 Month", One"2014-09-30 00:00:00-1 Month", A"2014-10-31 00:00:00-1 Month", -"2014-11-30 00:00:00-1 Month", -"2014-12-31 00:00:00-1 Month", the ); -  - foreach($DATEARR  as $val) - { +     $time=Strtotime($val); -     Echo[$time][$val]." \ r \ n "; +}

Let's take a look at the test results, and from the test results we've found that we're ignoring the difference in the number of days per month , so Strtotime () results in a different result.

So what exactly is Strtotime ("-$n month") calculated? Do a test as follows: Look at the results

1 
 Php2 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 Ten  while(1) One { A     if($flag+ > 12) -          Break; -  the     $time=Strtotime("-$flagMonth); -     $monthDiff= ($time-$tmp)/86400;//86400 = * *, -     $tmp=$time; -  +     $dispDate=Date("Y-m-d h:i:s",$time); -  +     Echo"{$flag} month ago: {$time}, Date: {$dispDate)} Difference Value: {$dispDate} days \ r \ n "; A}

(Note: Strtotime ("-$n Month"), the second parameter is omitted, the second parameter represents the time of the distance, the ellipsis represents the current time)

time

Difference

Theoretical time

results

July 31

January ago

June 31

June only 30 days, then add one day to July 1

July 31

February ago

May 31

July 31

March ago

April 31

April only 30 days, then add one day to May 1

...

So if so, how do we use Strtotime ("-$n month") to deal with our needs?

Here is a handwritten code for reference

1 /****************2 * Resolve the two time period between the month3 * $btm, $etm is a UNIX timestamp4 *****************/5 functionGettimedis ($BTM,$etm)6 {7     $RESARR=Array();8     if($etm<$BTM)9         return $RESARR;Ten  One     //turn both BTM and Etm to 1th per month . A     $BTMC=Strtotime(Date("Y-m-01 00:00:00",$BTM)); -     $ETMC=Strtotime(Date("Y-m-01 00:00:00",$etm)); -  the  -     $flag= 0;//Time Difference identifier -     $RESARR[] =Date("Ym",$ETMC); -  +      while(1) -     { +         $flag++; A         $compTime=Strtotime("-{$flag} month ",$ETMC); at          -         if($compTime<$BTM) -              Break; -  -         $RESARR[] =Date("Ym",$compTime); -     } in  -     return Array_unique($RESARR); to}


Problems with strtotime functions in PHP

Mktime () requires a parameter.

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 Strtotime's function description, you can also
+10 hours
-2 hours
Wait a minute.

How does the PHP strtotime function change at a specified time?

$date = Date ("y-m-d", Strtotime (' 2012-01-20 ') + 60*60*24);

http://www.bkjia.com/PHPjc/855628.html www.bkjia.com true http://www.bkjia.com/PHPjc/855628.html techarticle use PHP's Strtotime () function carefully, phpstrtotime function we in the daily business, for the business volume, often use the database by time to do horizontal sub-table, the query after the table will often ...

  • 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.