3.1.1. ProblemYou want to know what the time or date is. 3.1.2. SolutionUse strftime( ) or date( ) for a formatted time string, as in Example 3-2. Finding the current date and time
Example 3-2 prints: Wed May 10 18:29:59 2006 Wed, 10 May 2006 18:29:59 -0400 Use getdate( ) or localtime( ) if you want time parts. Example 3-3 shows how these functions work. Finding time parts
Example 3-3 prints: 18:23:45 18:23:45 3.1.3. DiscussionThe functions strftime( ) and date( ) can produce a variety of formatted time and date strings. They are discussed in more detail in Recipe 3.4. Both localtime( ) and getdate( ), on the other hand, return arrays whose elements are the different pieces of the specified date and time. The associative array getdate( ) returns the key/value pairs listed in Table 3-1.
Example 3-4 shows how to use getdate( ) to print out the month, day, and year. Finding the month, day, and year
Example 3-4 prints: May 5, 2007 Pass getdate( ) an epoch timestamp as an argument to make the returned array the appropriate values for local time at that timestamp. The month, day, and year at epoch timestamp 163727100 is shown in Example 3-5. getdate( ) with a specific timestamp
Example 3-5 prints: March 10, 1975 The function localtime( ) returns an array of time and date parts. It also takes an epoch timestamp as an optional first argument, as well as a boolean as an optional second argument. If that second argument is true, localtime( ) returns an associative array instead of a numerically indexed array. The keys of that array are the same as the members of the tm_struct structure that the C function localtime( ) returns, as shown in Table 3-2.
Example 3-6 shows how to use localtime( ) to print out today's date in month/day/year format. Using localtime( )
Example 3-6 prints: 6/23/2006 The month is incremented by 1 before printing since localtime( ) starts counting months with 0 for January, but we want to display 1 if the current month is January. Similarly, the year is incremented by 1900 because localtime( ) starts counting years with 0 for 1900. 3.1.4. See AlsoDocumentation on strftime( ) at http://www.php.net/strftime, date( ) at http://www.php.net/date, getdate( ) at http://www.php.net/getdate, and localtime( ) at http://www.php.net/localtime . |