Is there a function that is subtracted in the PHP
Directly is not correct, you need to do this: ** Get the current date and time in UNIX, the time representation is calculated from the time from January 1, 1970, this is called For UNIX Timestamps (UNIX EPOCH). If we have such a code: Echo time ();?> Time at this time at this time at 12:43 on May 21, 2000. You may say this is quite good. When this is helpless, or only one thing help. In PHP, functions for date processing must be used to use timestamp values returned by TIME (). At the same time, because PHP uses the same timestamp value in UNIX and Windows systems, this allows you to transplant between different systems without modifying the code. Another benefit is that the time () function returns an integer that you can store it as an integer field or text field into the database without having to use a special date / time field. You have basically understanding the timestamp value of UNIX, now let us show its practical use. Change the Date Display Method - Date and Time Display Form PHP provides two ways to convert UNIX timestamp values to useful data. The first is a DATE () function. This function has two parameters - the first string is used to set the format you want to return, the second is the timestamp value of UNIX. The formatted string displays the date and time you want to see by some simple special format characters. Suppose you want the date to display "18H01 Sunday 21 May" in this format. We need to use a special formatted character for each part of the string, and you can find it from the PHP manual. Such special formatted characters have a lot of characters, and they are similar to the week, the month of the English name, the year represented by 2 or 4 digits, whether it is morning (AM) or afternoon (PM) and others. For this example we need special characters: 'h'-24 hours of hours 'i'- minutes' l'- week's English full name 'D'- the first few days of this month' F'-month English full name, our formatted string is "HHI L D f", the PHP code is: Echo date ("hhi ld f", time ());?> When we execute this code, we found us The result of the result is: 180609 Sunday 21 MAY is a bit strange. Let us check the PHP manual, the original number of Her's number of hours represents 12 hours. This once again proves a truth: "Computer only do what you tell it, not what you want it." We have two options. The first is to use the escape character "/" before h: Echo Date ("H / Hi L D f", Time ()); we get this result: 18h12 Sunday 21 May This is what we want. But if we need to include the date and time in a very complex sentence, do we need to use escape characters for each character? The answer is of course not. We use another function strftime (). StrfTime () has two benefits. The first benefit is not within the scope of this article - if you use the setLocale () function, you can get the name of the sound of the corresponding language via StrFTIME. Another benefit is that you can include special dates and time format characters in your string. This also means that you have to learn a complete set of formatted characters regardless of all special format characters you want to learn the Date () function. StrFTIME () works and Date () has no difference, except for the front of the special formatted characters, a percentage must be added.
If you use the strftime () function, the code of the foregoing example is as follows: Echo strftime ("% HH% M% a% D% B", TIME ());?> The result is: 18H24 Sunday 21 May, this may look Simplify, but consider if you need to display "Today Is Sunday 21 May 2000. The Time Is Somewhere Close TO 18H24." I want to use the Date () function, it is undoubtedly bored. At the beginning, I mentioned that we have two ways to get useful data from the UNIX timestamp value. We have just learned Date () and StrfTime (). Another getDate (). This function requires only UNIX timestamp values as parameters, and the return value of the function is the array of dates and times. Here is an example: $ DATE_TIME_ARRAY = getDate (time ()); Echo $ DATE_TIME_ARRAY ["weekday"];?> The result is: Sunday In addition to "WeekDay", the other part of the array is: "Seconds" - Second "Minutes" - Division "Hours" - Hours "MDAY" - "WDAY" this month - this week (number) "MON" - month (number) "Year" - Year "YDay" - R The first few days (numbers) "MONTH" - the full name we can now get easy to identify the date and time. So what else? ** Conversion The current dates of Unix usually you must handle data in some dates or time formats. Open an Access database of M $, all the dates are stored in the format of YYYY / MM / DD, and the current date is 20005/27. The mktime () function can convert a time to UNIX timestamp values. Format format: int mktime . The last parameter is used to specify whether you are in summertime, this parameter is optional, so we will ignore it. The code is as follows: Echo mktime (0, 0, 0, 5, 27, 2000);?> Because I don't know the hour, division and seconds, these parameters must be filled in, I set it to 0. Set to 0 means time is midnight. $ Access_date = "2000/05/27"; // Explode () function uses a string as the boundary to break down another string.
This example $ access_date to decompose $ DATE_ELEMENTS = Explode ("/", $ access_date); // This time // $ DATE_EEMENTS [0] = 2000 // $ DATE_ELEments [1] = 5 // $ Date_elements [2] = 27 Echo Mktime (0, 0, 0, $ DATE_EEMENTS [1], $ DATE_EEMENTS [2], $ DATE_ELEments [0]);?> We see a more complex situation than from Access database We get the date and time of the following format: 2000/05/27 02:40:21 PM // A string from Access $ DATE_TIME_STRING = "2000/05/27 02:40:21 PM"; // Decompose strings into 3 parts - Date, time, and morning / afternoon $ dt_elements = expenented; // Decomposition Date $ DATE_EEMENTS = EXPLODE ("/", $ dt_elements [0]); // Decomposition Time $ TIME_EMENTS = EXPLODE ("::", $ dt_elements [1]); // If it is in the afternoon, we increase time 12 hours to get 24 hours of time IF ($ dt_elements [2] == "pm") { $ TIME_ELEMENTS [0] = 12;} // output Echo mktime ($ TIME_ELEments [0], $ TIME_EEMENTS [1], $ TIME_EEMENTS [2], $ DATE_ELEments [1], $ DATE_ELEments [2], $ DATE_ELEments [ 0]);?> ** Sometimes we need to know what time after 6 hours, 35 days ago or how many seconds have been in the last time Quake3. We already know how to get UNIX timestamp values from separate dates and times with mktime () functions. What should we do if we need a new date and time a UNIX timestamp value? Here are some exercises to help explain what we have to do will. As seen in the previous, mktime () uses the following parameters: hours, minutes, seconds, months, day and year. Think about the second section, the getDate () function can get these parameters for us. // Put the current timestamp value in a group $ TIMESTAMP = Time (); Echo $ TIMESTAMP; ECHO "
"; $ DATE_TIME_ARRAY = GetDate ($ timestamp); // Use the mktime () function Re-re- Generate UNIX timestamp value $ timestamp = mktime ($ DATE_TIME_ARRAY ["Hours"], $ DATE_TIME_ARRAY ["Minutes"], $ DATE_TIME_ARRAY ["Seconds"], $ DATE_TIME_ARRAY ["MON"], $ DATE_TIME_ARRAY ["mday"], $ DATE_TIME_ARRAY ["Year"]); Echo $ timestamp;?> It seems to be confused. I will use some variables to make the above program look more easily.
// Put the current timestamp value in a number of $ timestamp = time (); echo $ timestamp; echo "
"; $ date_time_Array = getDate ($ timestamp); $ Hours = $ date_time_Array ["Hours "]; $ Minutes = $ date_time_Array [" minutes "]; $ seconds = $ date_time_Array [" Seconds "]; $ month = $ date_time_Array [" MON "]; $ day = $ date_time_Array [" mday "]; $ year = $ DATE_TIME_ARRAY ["Year"]; // Renovate UNIX Timestamp = MKTIME ($ MONTH, $ DAY, $ 2, $ MONTH, $ DAY, $ 2; Echo $ TIMESTAMP; > Now we will put the corresponding name variable from the timestamp value generated by getDate (), so the code becomes relatively easy to read and understand. Now if we need to add 19 hours on current time, we use $ Hours 19 to replace $ Hours in the mktime () function. Mktime () will automatically transfer time to the next day. // put the current timestamp value in a population $ TIMESTAMP = Time (); Echo strftime ("% HH% M% a% D% B", $ TIMESTAMP); ECHO "
"; date_time_array = getdate ($ timestamp); $ hours = $ date_time_array [ "hours"]; $ minutes = $ date_time_array [ "minutes"]; $ seconds = $ date_time_array [ "seconds"]; $ month = $ date_time_array [ "mon" ]; $ Day = $ date_time_Array ["mday"]; $ year = $ date_time_Array ["year"]; // Re-generate Unix timestamp value // increase 19 hours $ TIMESTAMP = mktime ($ Hours "with mktime () function 19, $ MINUTES, $ Seconds, $ MONTH, $ day, $ year); Echo strftime ("% HH% M% a% D% B", $ TIMESTAMP); Echo "
~ e after address 19 hours" After running: 14H58 Saturday 03 Jun 09H58 Sunday 04 Jun ~ e After adding 19 Hours Reduction time is also the same - you only need to reduce the value of the corresponding variables. It is also very simple to get two different time values. What you need to do is just converting two time values to UNIX timestamp values, and then the two are reduced. The difference between the two is the number of seconds separated by two times. Other algorithms can quickly turn seconds to days, hours, points and seconds. ** Add a dateAdd function for the PHP.
In the introduction, how the PHP handles the date and time, let us transplant the two functions used in the ASP to PHP. The first function is DATEADD. Depending on VBScript documentation, the DATEADD (Interval, Number, Date) function is defined as "Returns the date that has been added to specify the time interval." INETRVAL is the time interval character, such as the time, or the day; Number is indicating Numerical expression of the number of time intervals; Date represents the date. Interval (time interval string expression) can be the following: YYYY YEAR Year Year Q Quarter M Month Month Y Day of Year One Year D Day Day W Weekday WW Week Of Year Week H Hour Hour N Minute The role of S Second Second W, Y and D is exactly the same, that is, add one day on the current date, Q plus 3 months, WW plus 7 days.
Function Dateadd ($ DATE_TIME_ARRAY = Getdate ($ DATE); $ Hours = $ DATE_TIME_AURRAY ["Hours"]; $ minutes = $ date_time_Array ["minutes"]; $ seconds = $ Date_time_Array ["Seconds"]; $ MONTH = $ DATE_TIME_ARRAY ["MON"]; $ day = $ date_time_Array ["mday"]; $ year = $ date_time_Array ["Year"]; switch ($ interval) {casse "yyyy" : $ YEAR = $ Number; Break; Case "Q": $ MONTH = ($ Number * 3); Break; Case "M": $ MONTH = $ Number; Break; Case "Y": Case "D ": Case" W ": $ day = $ Number; Break; Case" WW ": $ day = ($ Number * 7); Break; Case" H ": $ Hours = $ Number; Break; Case" N ": $ minutes = $ Number; Break; Case "S": $ Seconds = $ Number; Break; $ TIMESTAMP = MKTIME ($ Hours, $ MINUTES, $ Seconds, $ MONTH, $ DAY, $ YEAR); Return $ TIMESTAMP; }?> We can save the above code as a dateadd.inc file, then run the following code: Include ('dateadd.inc'); $ TEMPTIME = Time (); echo strftime ("% hH% M% a% D% B ", $ Temptime); $ TEMPTIME = DATEADD (" N ", 50, $ Temptime); ECHO"
"; Echo strftime ("% hH% M% a% D% B ", $ TEMPTIME) ;> We will get: 15H41 Saturday 03 Jun 16h31 Saturday 03 JUN Add a datediff function to the PHP now dateadd has been completed, then Datediff? Depending on the document, the Datediff (Interval, Date1, Date2) function is defined as "The time interval between the two dates" is returned. The usage of the intervals parameter is the same as the DATEADD function. For avoiding too complicated considerations, we decided to ignore other complex parameters in the Datediff function in VBScript, ie their two optional parameter variables [firstDayofweek [, firstweekofyear]] (they are used to determine the first day on the week is Sunday, The constant of the first week of Monday and a year.