Приглашаем посетить
Perl (perl.find-info.ru)

9.2 Parsing a Date or Time

Previous Table of Contents Next

9.2 Parsing a Date or Time

To work with date or time values in your program as epoch timestamps, you need to convert other time representations to epoch timestamps. If you have discrete date or time parts (for example, from different form parameters), then use mktime( ). It accepts an hour, minute, second, month, day, and year, and returns the corresponding epoch timestamp. Example 9-6 shows mktime( ) at work.

Example 9-6. Making an epoch timestamp
// get the values from a form

$user_date = mktime($_POST['hour'], $_POST['minute'], 0, $_POST['month'], $_POST['day'],

$_POST['year']);



// 1:30 pm (and 45 seconds) on October 20, 1982

$afternoon = mktime(13,30,45,10,20,1982);



print strftime('At %I:%M:%S on %m/%d/%y, ', $afternoon);

print "$afternoon seconds have elapsed since 1/1/1970.";

Example 9-6 prints:

At 01:30:45 on 10/20/82, 403983045 seconds have elapsed since 1/1/1970.

All of mktime( )'s arguments are optional. Whatever is left out defaults to the current date or time. For example, mktime(15,30,0) returns the epoch timestamp for 3:30 p.m. today, and mktime(15,30,0,6,5) returns the epoch timestamp for 3:30 p.m. on June 5th of this year.

When you want the epoch timestamp for something relative to a time you know, use strtotime( ). It understands English descriptions of relative times and returns an appropriate epoch timestamp. Example 9-7 shows how to find the epoch timestamp for some dates with strtotime( ).

Example 9-7. Using strtotime( )
$now = time( );

$later = strtotime('Thursday',$now);

$before = strtotime('last thursday',$now);

print strftime("now: %c \n", $now);

print strftime("later: %c \n", $later);

print strftime("before: %c \n", $before);

At noon on October 20, 2004, Example 9-7 prints:

now: Wed Oct 20 12:00:00 2004 

later: Thu Oct 21 00:00:00 2004 

before: Thu Oct 14 00:00:00 2004

Like date( ) and strftime( ), strtotime( ) also accepts an epoch timestamp second argument to use as the starting point for its calculations. Example 9-8 uses mktime( ) and strtotime( ) to find when the U.S. presidential election will be in 2008. U.S. presidential elections are held the Tuesday after the first Monday in November.

Example 9-8. Using strtotime( ) with a starting epoch timestamp
// Find the epoch timestamp for November 1, 2008

$november = mktime(0,0,0,11,1,2008);

// Find the First monday on or after November 1, 2008

$monday = strtotime('Monday', $november);

// Skip ahead one day to the Tuesday after the first Monday

$election_day = strtotime('+1 day', $monday);



print strftime('Election day is %A, %B %d, %Y', $election_day);

Example 9-8 prints:

Election day is Tuesday, November 04, 2008

The grammar that strtotime( ) follows is comprehensive but complicated to explain. The best way to familiarize yourself with it is to experiment. If you want to dig into the nitty gritty and see a list of everything that strtotime( ) can understand, read http://www.gnu.org/software/tar/manual/html_chapter/tar_7.html.

    Previous Table of Contents Next