| | |||||||
| |||||||
| |||||||
|
|
|
|
|
|
|
Combining Date Functions in PHP(Page 1 of 2 ) In this second part of an eight-part article series on working with date and time with PHP, you'll learn how to use the date() function, among others, and how to combine these functions for very effective and powerful results. This article is excerpted from chapter 12 of the book Beginning PHP and PostgreSQL 8: From Novice to Professional, written by W. Jason Gilmore and Robert H. Treat (Apress; ISBN: 1590595475). Working with Time The date() function can also produce time-related values. Let’s run through a few examples, starting with simply outputting the present time: echo "The time is ".date("h:i:s"); But is it morning or evening? Just add theaparameter: echo "The time is ".date("h:i:sa"); getdate() array getdate ([int timestamp]) Thegetdate()function returns an associative array consisting of timestamp components. This function returns these components based on the present date and time unless a Unix-format timestamp is provided. In total, 11 array elements are returned, including:
Caution The Windows operating system doesn’t support negative timestamp values, so the earliest date you could parse with this function on Windows is midnight, January 1, 1970. Consider the timestamp 1114284300 (April 23, 2005 15:25:00 EDT). Let’s pass it togetdate()and review the array elements: Array ( gettimeofday() mixed gettimeofday ([bool return_float]) Thegettimeofday()function returns an associative array consisting of elements regarding the current time. For those running PHP 5.1.0 and newer, the optional parameterreturn_floatcausesgettimeofday()to return the current time as a float value. In total, four elements are returned, including:
Executinggettimeofday()from a test server on April 23, 2005 16:24:55 EDT produces the following output: --------------------------------------------Array ( Of course, it’s possible to assign the output to an array and then reference each element as necessary: $time = gettimeofday(); This returns the following: Server location is 5 hours west of GMT. mktime() int mktime ([int hour [, int minute [, int second [, int month Themktime()function is useful for producing a timestamp, in seconds, between the Unix epoch and a given date and time. The purpose of each optional parameter should be obvious, save for perhapsis_dst, which should be set to 1 if daylight savings time is in effect, 0 if not, or –1 (default) if you’re not sure. The default value prompts PHP to try to determine whether daylight savings is in effect. For example, if you want to know the timestamp for April 27, 2005 8:50 p.m., all you have to do is plug in the appropriate values: echo mktime(20,50,00,4,27,2005); This returns the following: 1114649400 This is particularly useful for calculating the difference between two points in time. For instance, how many hours are there between now and midnight April 15, 2006 (the next major U.S. tax day)? $now = mktime(); // Difference in seconds // Calculate total hours echo "Only $hours hours until tax day!"; This returns the following: --------------------------------------------Only 8451 hours until tax day! time() int time() Thetime()function is useful for retrieving the present Unix timestamp. The following example was executed at 15:25:00 EDT on April 23, 2005: echo time(); This produces the following: 1114284300 Using the previously introduceddate()function, this timestamp can later be converted back to a human-readable date: echo date("F d, Y h:i:s", 1114284300); This returns the following: April 23, 2005 03:25:00 If you’d like to convert a specific date/time value to its corresponding timestamp, see the previous section formktime(). More Programming Basics Articles |
| |
| |