PHP5: Time/Date

Posted on February 8, 2011

PHP5 has some new tricks up its sleeve when it comes to dates and times. Most of these additions come in the form of new functions.

First off, PHP5 adds date_sunrise() and date_sunset(). As you might guess, these functions return the time of the sunrise or sunset for a given day and location (given in latitude and longitude). The value returned can be in “hh:mm” form, a floating-point number, or a timestamp.

In version 5, PHP adds time_nanosleep() to the collection of sleep functions. This function allows you to have even more granularity, in theory down to a billionth of a second. I’m not sure why, after having sleep() and usleep(), they didn’t name this one “nsleep”.

The idate() function is similar to the old date() function, but rather than returning a formatted string, it returns a formatted integer. The parameters are basically the same: the format character(s) and an optional timestamp. For idate(), though, only 1 formatting character is allowed (since only 1 integer is returned). I’m not sure how useful this function is over date() though, since PHP is loosely typed and idate() doesn’t seem to have any formatting characters that date() doesn’t have. Return values would strip any leading zeroes, and idate() is probably preferable in situations where an integer matches the semantics surrounding it.

Thankfully, PHP5 adds more support for timezones. In PHP5, you need to set a timezone for your scripts, either in the date.timezone INI setting or by using date_default_timezone_set(). (There are other ways, but these are the preferred methods.)

PHP5 also includes an entire date/time extension by default. In fact, there are object representations of the date and timezone, named DateTime and DateTimeZone respectively. In addition to having an object representation, many of their methods map to existing procedural date functions and add new functions. The pseudo-utility classes of DateInterval and DatePeriod are also included.

For example, with DateTime objects you can perform math operations such as adding, subtracting, incremental changes, and comparing dates (and even use comparison operators like <, >, and ==).

The DateTimeZone class tells you almost anything you’d need to know about time zones. For example, the getTransitions() method will tell you when daylight savings time goes into effect:

$theTime = time();
$tz = new DateTimeZone('America/Chicago');
$transition = $tz->getTransitions($theTime, $theTime+60*60*24*365);

/*
$transition is now:
Array
(
  [0] => Array
  ( 
    [ts] => 1297114801
    [time] => 2011-02-07T21:40:01+0000
    [offset] => -21600
    [isdst] =>
    [abbr] => CST
  )

  [1] => Array
  (
    [ts] => 1300003200
    [time] => 2011-03-13T08:00:00+0000
    [offset] => -18000
    [isdst] => 1
    [abbr] => CDT
  )

  [2] => Array
  (
    [ts] => 1320562800
    [time] => 2011-11-06T07:00:00+0000
    [offset] => -21600
    [isdst] =>
    [abbr] => CST
  )
)
*/

PHP5 adds some new parameters for date() formatting as well:

  • “N”, like “w”, is for the day of the week, but here “1” is Monday and “7” is Sunday
  • “o” is the same as “Y”, except in border cases where the current week belongs to the previous or next year
  • “u” for the number of microseconds (always “000000” unless you are using a DateTime object)
  • “e” for the timezone identifier (e.g. “CST”)
  • “P” for timezone offsets is like “O” but places a colon between the hours and minutes (e.g. “-06:00”)
  • “c” for the ISO 8601 date (e.g., “2004-02-12T15:19:21+00:00”)

It’s now easier to parse dates and times, using the new date_parse_from_format() function. The function returns an associative array of parts, plus any warnings or errors encountered:

$date = "6.1.2009 13:00+01:00";
print_r(date_parse_from_format("j.n.Y H:iP", $date));
/*
Prints out:
Array
(
  [year] => 2009
  [month] => 1
  [day] => 6
  [hour] => 13
  [minute] => 0
  [second] => 0
  [fraction] =>
  [warning_count] => 0
  [warnings] => Array
  (
  )
  [error_count] => 0
  [errors] => Array
  (
  )
  [is_localtime] => 1
  [zone_type] => 1
  [zone] => -60
  [is_dst] =>
)
*/

A utility function called timezone_name_from_abbr() is also available for converting a timezone abbreviation or offset to the full name:

echo timezone_name_from_abbr("CET"); // prints "Europe/Berlin"
echo timezone_name_from_abbr("", 3600, 0); // prints "Europe/Paris"

There’s more, but these are the most significant additions and highlights that I could find.

Leave a Reply

  1.  

    |