php has an in depth understanding of the usage of strtotime functions

  • 2020-06-01 09:18:42
  • OfStack

In the previous < How to use PHP to calculate up to 1 month today > 1 in the article, we mentioned that the strtotime function will have some problems when using strtotime(" -1 month ") to solve the problem today in one month.
This article includes the following:
The & # 8226; Some usage of the strtotime function
The & # 8226; The basic principle of strtotime function
The & # 8226; Why the evaluation of strtotime(" -1 month ") failed
Some usage of the strtotime function
1. strtotime(" JAN ") and strtotime(" January ")
Both of these usages have the effect of 1, which is to return the date of the specified month. If the specified month does not have today, it will be postponed to the next month. As shown in the calculation of February on 2011-03-31, the code is:

echo date("Y-m-d H:i:s", strtotime("feb", strtotime("2011-03-31")));

The program will output: 2011-03-03 00:00:00. From the appearance, this result may not be what we want, but it is also a solution. What determines this solution? The strtotime function only calculates the month when performing the month calculation, which is equivalent to setting the month directly to the value of the specified month. For example, jan and january will have a corresponding internal value.
2. first keyword
first is an auxiliary keyword, which can be used in combination with the keyword of week, day, etc., which can specify the confirmation value. For example, find the first Sunday of 2011:

echo date("Y-m-d H:i:s", strtotime("second sunday", strtotime("2011-01-01"))), "<br />";  

In the source code of PHP, the combination of first and week and day is separated, that is, first day corresponds to one processing operation. In the final C implementation, the value of day is specified as 1, that is, the d field in time structure is specified as 1, as follows:

switch (time->relative.first_last_day_of) {  
         case 1: /* first */  
             time->d = 1;  
             break;  
         case 2: /* last */  
             time->d = 0;  
             time->m++;  
             break;  
     }

3, previous and next keywords
Similar to first, the previous keyword can be used in combination with days of the week and days of the week to indicate the day of the previous week or the previous day of a specified time. The following code is shown:

echo date("Y-m-d H:i:s", strtotime("previous sunday", strtotime("2011-02-01"))), "<br />";  

The program will output: 2011-01-30 00:00:00
The program seeks the first Sunday before February 1, 2011.
The next keyword is the opposite of previous, which means the day of the next week or the day after.
4. last keyword
The last keyword can be used as either the previous or the last. For example, the date of last Sunday:

echo date("Y-m-d H:i:s", strtotime("last sunday", strtotime("2011-02-05"))), "<br />";  

The program will output: 2011-01-30 00:00:00
When the program is the last, its application scenario is the last day of the month in which the specified date is located, equivalent to the result of date(" t "). For the last day of February 2000:

echo date("Y-m-d H:i:s", strtotime("last day", strtotime("2000-02-01"))), "<br />";  

The first, previous, last, and this keywords belong to the same group in the re file.
5, back and front keywords
These two keywords are the forward and backward operations for the hour in 1 day, and the call format is as follows:

echo date("Y-m-d H:i:s", strtotime("back of 24", strtotime("2011-02-01"))), "<br />";  
echo date("Y-m-d H:i:s", strtotime("front of 24", strtotime("2011-02-01"))), "<br />";  

The & # 8226; back represents the 15-minute position of the hour after setting the time to the specified hour value. If it's 24, it goes up to zero fifteen on the second day.
The & # 8226; front represents setting the time at 45 minutes of the first hour of the specified hour value. If it's 0, then it's 23:45 from the previous day.
The above code output: 2011-02-02 00:15:00 2011-02-01 23:45:00. The array following back of and front of must be greater than or equal to 0 and less than or equal to 24.
The basic principle of strtotime function
The official documentation for the strtotime function says this: this function expects to take a string containing an American English date format and attempts to parse it into Unix timestamp (seconds from January 1 1970 00:00:00 GMT), its value relative to the time given by the now parameter, or the system current time if no such parameter is provided.
This is a standard PHP built-in function that has been around since PHP4. The strtotime function is loaded as an extension and is fully implemented in the ext/date directory. As a standard built-in function, its definition format is also standard, as follows:

PHP_FUNCTION(strtotime)  
//   Process the input for whether or not there is an input 2 There is no processing of the parameters   
 //   Call the related function, the implementation of string parsing and result calculation   
//   Returns the result   
}  

In the input processing, the case where both parameters exist is first identified and processed. If not, the case where the second parameter does not exist will be processed. If neither of them exists, an error will be reported and FALSE will be returned.
The first parameter of the strtotime function is a string. For this string, PHP USES its lexical parsing tool: re2c due to its complexity. In the /ext/date/lib directory, we can see the original re file from the parse_date.re file. When the user passes in a string in the form of a parameter, the string is passed to the program for processing, matching different processing functions according to its string. If strtotime(" yesterday ") is called, when analyzing the string, the yesterday string will be matched, and this string corresponds to the function as follows:

'yesterday'  
     {  
         DEBUG_OUTPUT("yesterday");  
         TIMELIB_INIT;  
         TIMELIB_HAVE_RELATIVE();  
         TIMELIB_UNHAVE_TIME();  
         s->time->relative.d = -1;  
         TIMELIB_DEINIT;  
         return TIMELIB_RELATIVE;  
     }

Here are a few key structures:

typedef struct Scanner {  
         int           fd;  
         uchar        *lim, *str, *ptr, *cur, *tok, *pos;  
         unsigned int  line, len;  
         struct timelib_error_container *errors;  
         struct timelib_time *time;  
         const timelib_tzdb  *tzdb;  
     } Scanner;  
     typedef struct timelib_time {  
         timelib_sll      y, m, d;     /* Year, Month, Day */  
         timelib_sll      h, i, s;     /* Hour, mInute, Second */  
         double           f;           /* Fraction */  
         int              z;           /* GMT offset in minutes */  
         char            *tz_abbr;     /* Timezone abbreviation (display only) */  
         timelib_tzinfo  *tz_info;     /* Timezone structure */  
         signed int       dst;         /* Flag if we were parsing a DST zone */  
         timelib_rel_time relative;  
         timelib_sll      sse;         /* Seconds since epoch */  
         unsigned int   have_time, have_date, have_zone, have_relative, have_weeknr_day;  
         unsigned int   sse_uptodate; /* !0 if the sse member is up to date with the date/time members */  
         unsigned int   tim_uptodate; /* !0 if the date/time members are up to date with the sse member */  
         unsigned int   is_localtime; /*  1 if the current struct represents localtime, 0 if it is in GMT */  
         unsigned int   zone_type;    /*  1 time offset, 
                                       *  3 TimeZone identifier, 
                                       *  2 TimeZone abbreviation */  
     } timelib_time;  
     typedef struct timelib_rel_time {  
         timelib_sll y, m, d; /* Years, Months and Days */  
         timelib_sll h, i, s; /* Hours, mInutes and Seconds */  
         int weekday; /* Stores the day in 'next monday' */  
         int weekday_behavior; /* 0: the current day should *not* be counted when advancing forwards; 1: the current day *should* be counted */  
         int first_last_day_of;  
         int invert; /* Whether the difference should be inverted */  
         timelib_sll days; /* Contains the number of *days*, instead of Y-M-D differences */  
         timelib_special  special;  
         unsigned int   have_weekday_relative, have_special_relative;  
     } timelib_rel_time;

Reason for failure of evaluation of strtotime(" -1 month ")
Although this method, strtotime(" -1 month "), fails to evaluate the number of days in the next month compared to the previous month, it is not wrong in nature. There's nothing wrong with this implementation of PHP. It's just that our requirements dictate that we can't use this method, so we call it an evaluation failure.
Let's take a look at its implementation. Since there is no second parameter, the program USES the default current time. The first parameter is passed in the -1 month string, and the regularity in the re file corresponding to this string is:

echo date("Y-m-d H:i:s", strtotime("second sunday", strtotime("2011-01-01"))), "<br />";  
0
Eventually, relative will correspond to a series of 1 operations, and the program will recognize the previous -1 and the month string. month corresponds to a type of operation: TIMELIB_MONTH. After that, the operation is performed according to the identified number and operation type, as follows:

case TIMELIB_MONTH:  s->time->relative.m += amount * relunit->multiplier; break;  

In the code above, the relative value of the directly recorded month is reduced by 1. But in a situation like March 31, where there is no 31st in February, the program will automatically calculate the date to the next month.

Related articles: