Programming Basics

  Home arrow Programming Basics arrow Combining Date Functions in PHP

Combining Date Functions in PHP
By: Apress Publishing
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating:  stars stars stars stars stars / 0

    Table of Contents:
  • Combining Date Functions in PHP
  • Date Fu



    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");
    // The time is 07:44:53

    But is it morning or evening? Just add theaparameter:

    echo "The time is ".date("h:i:sa");
    // The time is 07:44:53pm


    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:

    1. hours: Numeric representation of the hours. The range is 0 through 23.
    2. mday: Numeric representation of the day of the month. The range is 1 through 31. 
    3. minutes: Numeric representation of the minutes. The range is 0 through 59. 
    4. mon: Numeric representation of the month. The range is 1 through 12. 
    5. month: Complete text representation of the month, e.g. July. 
    6. seconds: Numeric representation of seconds. The range is 0 through 59. 
    7. wday: Numeric representation of the day of the week, e.g. 0 for Sunday. 
    8. weekday: Complete text representation of the day of the week, e.g. Friday. 
    9. yday: Numeric offset of the day of the year. The range is 0 through 365. 
    10. year: Four-digit numeric representation of the year, e.g. 2005. 
    11. 0: Number of seconds since the Unix epoch. While the range is system-dependent, on Unix-based systems, itís generally Ė2147483648 through 2147483647, and on Windows, the range is 0 through 2147483648.  

    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 (
     [seconds] => 0
     [minutes] => 25
     [hours] => 15
     [mday] => 23
     [wday] => 6
     [mon] => 4
     [year] => 2005
     [yday] => 112
     [weekday] => Saturday
     [month] => April
    [0] => 1114284300


    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:

    1. dsttime: Indicates the daylight savings time algorithm used, which varies according to geographic location. There are 11 possible values, including 0 (no daylight savings time enforced), 1 (United States), 2 (Australia), 3 (Western Europe), 4 (Middle Europe), 5 (Eastern Europe), 6 (Canada), 7 (Great Britain and Ireland), 8 (Romania), 9 (Turkey), and 10 (the Australian 1986 variation). 
    2. minuteswest: The number of minutes west of Greenwich Mean Time (GMT). 
    3. sec: The number of seconds since the Unix epoch. 
    4. usec: The number of microseconds should the time fractionally supercede a whole second value.

    Executinggettimeofday()from a test server on April 23, 2005 16:24:55 EDT produces the following output:

    --------------------------------------------Array (
      [sec] => 1114287896
      [usec] => 110683
      [minuteswest] => 300
      [dsttime] => 1

    Of course, itís possible to assign the output to an array and then reference each element as necessary:

    $time = gettimeofday();
    $GMToffset = $time['minuteswest'] / 60;
    echo "Server location is $GMToffset hours west of GMT.";

    This returns the following:

    Server location is 5 hours west of GMT.


    int mktime ([int hour [, int minute [, int second [, int month
    [, int day [, int year [, int is_dst]]]]]]])

    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:


    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();
    $taxday = mktime(0,0,0,4,15,2006);

    // Difference in seconds
    $difference = $taxday - $now;

    // Calculate total hours
    $hours = round($difference / 60 / 60);

    echo "Only $hours hours until tax day!";

    This returns the following:

    --------------------------------------------Only 8451 hours until tax day!


    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:


    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
    More By Apress Publishing

    blog comments powered by Disqus


    - The Transliteration Operator in Perl
    - Perl String Processing Functions
    - Perl String Processing
    - Control Flow Constructs: Loops Conclusion
    - Loop Control Constructs
    - Control Flow Constructs: the For and Foreach...
    - Loops and Control Flow Constructs
    - Expression Modifiers for Perl Control Flow C...
    - Logical Operators and Control Flow Constructs
    - Comparing Strings with Control Flow Construc...
    - Perl Operators and Control Flow Constructs
    - Control Flow Constructs
    - More Time Manipulation with PHP
    - Validating and Manipulating Dates with PHP
    - Using the Date Constructor in PHP

    Developer Shed Affiliates


    © 2003-2019 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap