< Day Day Up > |
9.1 Displaying the Date or TimeThe simplest display of date or time is telling your users what time it is. Use the date( ) or strftime( ) function as shown in Example 9-1. Example 9-1. What time is it?print 'strftime( ) says: '; print strftime('%c'); print "\n"; print 'date( ) says:'; print date('r'); At noon on October 20, 2004, Example 9-1 prints: strftime( ) says: Wed Oct 20 12:00:00 2004 date( ) says: Wed, 20 Oct 2004 12:00:00 -0400 Both strftime( ) and date( ) take two arguments. The first controls how the time or date string is formatted, and the second controls what time or date to use. If you leave out the second argument, as in Example 9-1, each uses the current time. With date( ), individual letters in the format string translate into certain time values. Example 9-2 prints out a month, day, and year with date( ). Example 9-2. Printing a formatted date string with date( )print date('m/d/y'); At noon on October 20, 2004, Example 9-2 prints: 10/20/04 In Example 9-2, the m becomes the month (10), the d becomes the day of the month (20), and the y becomes the two-digit year (04). Because the slash is not a format character that date( ) understands, it is left alone in the string that date( ) returns. With strftime( ), the things in the format string that get replaced by time and date values are set off by percent signs.[1] Example 9-3 prints out a month, day, and year with strftime( ).
Example 9-3. Printing a formatted date string with strftime( )print strftime('%m/%d/%y'); At noon on October 20, 2004, Example 9-3 prints: 10/20/04 In Example 9-3, the %m becomes the month, the %d becomes the day, and %y becomes the two-digit year. Table 9-1 lists all of the special characters that date( ) and strftime( ) understand. The "Windows?" column indicates whether the character is supported by strftime( ) on Windows.
As just mentioned, to get date( ) or strftime( ) to print a formatted time string for a particular time, supply that time (as an epoch timestamp) as the second argument to either function. Example 9-4 prints out the time an hour from now. It uses the time( ) function, which returns the current epoch timestamp. Example 9-4. Printing a formatted time string for a particular timeprint 'strftime says( ): '; print strftime('%I:%M:%S', time( ) + 60*60); print "\n"; print 'date( ) says: '; print date('h:i:s', time( ) + 60*60); At noon on October 20, 2004, Example 9-4 prints: strftime( ) says: 01:00:00 date( ) says: 01:00:00 At noon, time( ) + 60*60 equals the epoch timestamp for 1 p.m. (60*60 = 3600, the number of seconds in one hour.) The formatting characters used by strftime( ) and date( ) in Example 9-4 print the hour, minute, and second corresponding to the supplied epoch timestamp. The date( ) and strftime( ) functions each have their strong points. If you are generating a formatted time or date string that has other text in it too, strftime( ) is better because you don't have to worry about letters without percent signs turning into time or date values. Example 9-5 shows how to use date( ) and strftime( ) to print a formatted date string like this. The version with strftime( ) is simpler. Example 9-5. Printing a formatted time string with other textprint 'strftime( ) says: '; print strftime('Today is %m/%d/%y and the time is %I:%M:%S'); print "\n"; print 'date( ) says: '; print 'Today is ' . date('m/d/y') . ' and the time is ' . date('h:i:s'); At noon on October 20, 2004, Example 9-5 prints: strftime( ) says: Today is 10/20/2004 and the time is 12:00:00 date( ) says: Today is 10/20/2004 and the time is 12:00:00 The date( ) function shines for different reasons. It supports some things that strftime( ) doesn't, such as a leap year indicator, a DST indicator, and trimming leading zeroes from some values. Furthermore, date( ) is a PHP-specific function. The strftime( ) PHP function relies on an underlying operating system function (also called strftime( )). That's why some format characters aren't supported on Windows. When you use date( ), it's guaranteed to work the same everywhere. Unless you need to put text that isn't format characters into the format string, choose date( ) over strftime( ). |
< Day Day Up > |