11.2. Date and Time FunctionsPHP uses the standard Unix-style timestamp to work with dates. This is simply the number of seconds since January 1, 1970. You get the current timestamp using the time function, shown in Example 11-15. Example 11-15. A simple echo of the timestamp
This results in Figure 11-13. Figure 11-13. A Unix timestamp
This is not exactly the most meaningful representation of the date and time. So instead, you can use the date function to translate the timestamp into a meaningful string. The date function takes a timestamp and a format string, as shown in Example 11-16. Example 11-16. Making the date and time appear like we expect
This code returns the screen shown in Figure 11-14. Figure 11-14. An easy-to-read date and time from the date functionDates and times can be displayed in a variety of formats; these will be discussed next. 11.2.1. Display FormatsDate and times are displayed in a variety of formats.
11.2.2. ArithmeticAdding or subtracting days and hours can be done by adding or subtracting seconds. While this may sound odd, it's not hard. To add two days to a timestamp, add 2*24*60*60 (2 days*24 hours*60 minutes*60 seconds) to the timestamp, as shown in Example 11-17. Example 11-17. Adding two days to the date
This outputs:
Let's see what else you need to create dates with validation. 11.2.3. Validating DatesWhen you receive a user-supplied date, it's good practice, as with any other user-supplied data, to check that it's valid. You can use the checkdate function, shown in Example 11-18, to validate a date. It takes three parametersthe month, day, and yearfor a date to validate. If the date is valid, it returns trUE; otherwise, it returns FALSE. Example 11-18. Validating two dates
As you can tell by our example in Figure 11-15, the 31 April 2005 date was invalid, yet 31 May 2005 was valid. This can happen because of a typo or a user just entering wrong information. Figure 11-15. Only some months have 31 daysOnce you know that you have the valid segments of a date, you can create a timestamp. 11.2.4. Using mktime to Create a TimestampIt's fairly easy to get the current time and date using date, but if you're trying to create a date and all you have are the components of the date such as month, day, and year, you'll need to use the mktime function. The mktime function takes these parameters:
You can omit some of the parameters when calling mktime, and they'll be filled in from the current time. mktime is a timestamp, which is an integer containing the number of seconds between the Unix Epoch of January 1 1970 00:00:00 GMT and the time specified. You can't omit them out of order though. Example 11-19 checks whether the date is valid, and then creates a timestamp. Example 11-19. Creating a timestamp from the components of a date
When run, this code produces the screen shown in Figure 11-16. Figure 11-16. A timestamp created from its componentsNow that we've covered dates and times, it's time to head onto more exciting topics. Note that the number of the year might be a two- or four-digit value, between 0 to 69 mapping to 2000 to 2069 and 70 to 100 to 1970 to 2000. On systems where time_t is a 32-bit signed integer, which is most common today, the valid range for the year is somewhere between 1901 and 2038. This limitation is fixed since the release of PHP 5.1.0. |
Tuesday, October 27, 2009
Section 11.2. Date and Time Functions
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment