I am planning to write a help processing system with PHP. I found that I have to know how long it has been in the last customer's problem? When I have used ASP to solve this problem is quite simple, ASP has corresponding function DateDiff to give the two days of interval how many months, how many days and how many seconds. When I search for the PHP manual, I found that PHP did not have a similar function. This article contains the following: 1, get the current date and time - how many ways we have? 2, change the date display method - Date and time display form 3, the current date is the timestamp value of UNIX 4, change the date a. Increase time b. Minus time c. Find the interval between two days 5 Add a DATEADD function 6 for PHP, add a dateDiff function for PHP ** Get the current date and time in UNIX, time representation is the number of seconds from January 1, 1970, called UNIX Timestamp (UNIX EPOCH). If we have such a code:
Code: Echo Time ();?>
The return value of 958905820 is 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 so our formatted string is "HHI LD F", the PHP code is:
Code: Echo Date ("HHI L D f", Time ());?>
When we execute this code, we found the results we got to: 180609 Sunday 21 May, which looks a bit weird. 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:
Code: 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 previous example is as follows: Code: Echo strftime ("% HH% M% a% D% B", TIME ());?>>
The result is: 18H24 Sunday 21 May This may seem to simplify, but consider if you need to display "Today Is Sunday 21 May 200H24." I want to use the Date () function. Undoubtedly troubled. 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. Below is an example:
Code: $ DATE_TIME_ARRAY = getdate (time ()); echo $ date_time_Array ["weekday"];?>>
The result of returning is: SUNDAY, in addition to "Weekday", the other part of the array is: "Seconds" - Second "Minutes" - Division "Hours" - Hours "MDAY" - the first few days of this month "WDAY" - this week Day few (numbers) "MON" - month (number) "year" - Year "YDay" - R This year (number) "Month" - Month, we can now get an easy identification 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. code show as below:
Code: Echo mktime (0, 0, 0, 5, 27, 2000);?>
Since I don't know the hour, the seconds, the seconds are simultaneous, these parameters must be filled, I set it to 0. Set to 0 means time is midnight.
Code: $ Access_date = "2000/05/27"; // Explode () function uses a string as the boundary to decompose 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
Code: // 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 = evtern ( "", $ DATE_TIME_STRING); // Decomposition Date $ DATE_ELEments = EXPLODE ("/", $ dt_elements [0]); // Decomposition time $ TIME_EEMENTS = EXPLODE ("::", $ dt_elements [1]); // If This is in the afternoon, we increase the time to get the 24-hour time IF ($ dt_elements [2] == "PM") {$ TIME_EMENTS [0] = 12;} // Output Echo Mktime ($ TIME_EMENTS [ 0], $ TIME_EMENTS [1], $ TIME_ELEments [2], $ DATE_ELEments [1], $ DATE_EEMENTS [2], $ DATE_ELEments [0]);?>>>>>>>>>
** Date of modification Sometimes we need to know what time after 6 hours, 35 days ago or from your last play quake3 after you have passed how many seconds. 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.
Code: // Put the current timestamp value in a population $ TIMESTAMP = Time (); Echo $ TIMESTAMP; Echo "P"; $ DATE_TIME_ARRAY = GetDate ($ TIMESTAMP); // Re-use mktime () function 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 that there is some confusing. I will use some variables to make the above program look more easily.
Code: // Put the current timestamp value in a number of $ TIMESTAMP = Time (); Echo $ TIMESTAMP; Echo "P"; $ 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; 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.
Code: // Put the current timestamp value in a population $ TIMESTAMP = Time (); Echo strftime ("% HH% M% a% D% B", $ TIMESTAMP); ECHO "P"; $ 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 () functions 19, $ MINUTES, $ Seconds, $ MONTH, $ day, $ year); Echo strftime ("% HH% M% a% D% B", $ TIMESTAMP); Echo "Br ~ e instolds 19 hours";? >
Run: 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. Code: Function Dateadd ($ DATE_TIME_ARRAY = Getdate ($ DATE); $ Hours = $ DATE_TIME_AURRAY ["Hours"]; $ minity = $ 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:
Code: Include ('dateadd.inc'); $ TEMPTIME = Time (); Echo strftime ("% hH% M% a% D% B", $ TEMPTIME); $ TEMPTIME = DateAdd ("N", 50 , $ TEMPTIME); Echo "P"; Echo strftime ("% HH% M% a% D% B", $ TEMPTIME);?> We will get: 15h41 Saturday 03 Jun 16h31 Saturday 03 JUN Add a datediff function for 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, On Monday and the first week of the first week. And we only allow Intervals to have the following five values: "W" (week), "D" (day), "h" (hour), "n" (minutes) And "s" (second). Let's see what we can come UP with: The following code is what we need:
Code: Function datediff ($ interval, $ date1, $ date2) {// Get two days between two days $ TIMEDIFCERENCE = $ DATE2 - $ DATE1; Switch ($ Interval) {CASE "W": $ RETVAL = BCDIV ($ TIMEDIFMERENCE, 604800); Break; Case "D": $ RETVAL = BCDIV ($ TIMEDIFMERENCE, 86400); Break; Case "H": $ RETVAL = BCDIV ($ TIMEDIFMERENCE, 3600); Break; Case "N ": $ RETVAL = BCDIV ($ TIMEDIFMERENCE, 60); Break; Case" S ": $ RETVAL = $ TIMEDIFMERENCE; Break;} Return $ RETVAL;}?>
The above code is stored as a dateDiff.inc file and then run the following code:
Code: Include ('datediff.inc'); include ('dateadd.inc'); $ currentTime = time (); echo "current time:". Strftime ("% hH% M% a% D% B" $ CurrentTime). "br"; $ newtime = dateadd ("n", 50, $ currenttime); echo "Time Plus 50 minutes:". Strftime ("% HH% M% a% D% B", $ NewTime "Br"; $ TEMPTIME = Datediff ("N", $ CurrentTime, $ NewTime); Echo "Interval Between Two Times:". $ TEMPTIME;?> If everything goes well, you can see the following results: Current Time: 16H23 Saturday 03 Jun Time Plus 50 Minutes: 17h13 Saturday 03 Jun Interval Between Two Times: 50 If you run PHP on the UNIX machine, you must compile PHP support BC high precision functions. You must download the BC library from the following address http://www.php.net/extra/number4.tar.gz, then extract it to the root of PHP4, recompile PHP, add - to -ENABLE- BCMATH options. (Detailed explanation of Readme.BCMATH in PHP4). The Windows version of PHP4 does not need to do any patch, you can use the BC high precision function directly. Now you have got a function of dealing with the date and time, and the rest is how to apply it to your PHP program.