PhpDig.net

What is PhpDig?
PhpDig is a PHP MySQL based
Web Spider & Search Engine.




mktime

Name

mktime — Returns the UNIX timestamp for a date.

Synopsis

int mktime([hour][, min][, sec][, mon][, day][, year][, is_dst]);
int hour (optional): Numeric hours
int min (optional): Numeric minutes
int sec (optional): Numeric seconds
int mon (optional): Numeric month
int day (optional): Numeric day
int year (optional): Numeric year
int is_dst (optional): Whether Daylight Saving Time is active

Returns

UNIX timestamp if successful, or -1 if 0 is given for each of month, day, and year

Description

mktime() calculates the UNIX timestamp for the time and date given by its parameters. All parameters are optional; if a parameter isn't specified, the current time is used for that parameter. However, keep in mind that arguments can only be left off in order from right to left (for example, you can't leave out day without first leaving out year and is_dst ).

You can give invalid values to mktime() , and it will automatically calculate the correct value. For instance, if you specify the date April 32, mktime() will calculate it as May 2.

mktime() accepts the year parameter as either a two-digit or four-digit integer. However, any year value given must fall within the range supported by the system on which PHP is running. Most current systems use a 32-bit integer to store the date and time, meaning that mktime() will only work on dates within the range 1970-2037 (inclusive). Furthermore, if a two-digit value is given, values from 0 through 69 will be interpreted as the corresponding years from 2000 through 2069, while values from 70 through 99 will be interpreted as the corresponding years from 1970 through 1999.

is_dst tells mktime() whether Daylight Saving Time should be taken into account. There are three valid values: 1 means yes, 0 means no, and-1 means unknown. The default is-1.

Note that 0 can have different meanings, depending on the parameter for which you use it:

  • 0 for hour means hour 0 of the resulting day

  • 0 for minute means minute 0 of the resulting hour

  • 0 for second means second 0 of the resulting minute

  • 0 for month means the last month of the year immediately preceding the resulting year

  • 0 for day means the last day of the month immediately preceding the resulting month

  • 0 for year means the year 2000



This list has some interesting implications. For instance, it's always easy to find out what the last day of February is for any given year - simply ask for the 0th day of March:

/* Finds the last day of February for the current year. */
$timestamp = mktime(0, 0, 0, 3, 0)


However, it can also be confusing. For instance, consider the following:

/* What would this give? */
$timestamp = mktime(0, 0, 0, 0, 1, 0);




To follow the logic, note that 0 for year gives 2000, while 0 for month gives the last month of the preceding year, 1999. The 1 for day means to give the first day of that month, so you wind up with December 1, 1999.

However, PHP's implementation of mktime() will return -1 if you specify 0 for each of month , day , and year , not November 30, 1999, as you might expect.

Version

PHP 3, PHP 4

Example

Example 206. Various uses for mktime()

/* When tested at 22:21 on April 19 2001,
 * the following code produced this output:
 * 22:21:32 Thursday, Apr 19 2001
 * 22:21:32 Thursday, Apr 19 2001
 * 14:00:00 Wednesday, May  2 2001
 * 14:00:00 Monday, Apr 30 1973
 * 14:00:00 Wednesday, Apr 30 2025
 */

/* Print the current date and time. */
echo strftime("%X %A, %b %e %Y\n", mktime());

/* The above is the same as using time() for this purpose. */
echo strftime("%X %A, %b %e %Y\n", time());

/* Print the time and date at 2 PM April 32. 
 * mktime() will compensate for the fact that there is no April 32. */
echo strftime("%X %A, %b %e %Y\n", mktime(14, 0, 0, 4, 32));

/* The '73' year value produces a result year of 1973. */
echo strftime("%X %A, %b %e %Y\n", mktime(14, 0, 0, 4, 30, 73));

/* The '25' year value produces a result year of 2025. */
echo strftime("%X %A, %b %e %Y\n", mktime(14, 0, 0, 4, 30, 25));



PHP Functions Essential Reference. Copyright © 2002 by New Riders Publishing (Authors: Zak Greant, Graeme Merrall, Torben Wilson, Brett Michlitsch). This material may be distributed only subject to the terms and conditions set forth in the Open Publication License, v1.0 or later (the latest version is presently available at http://www.opencontent.org/openpub/). The authors of this book have elected not to choose any options under the OPL. This online book was obtained from http://www.fooassociates.com/phpfer/ and is designed to provide information about the PHP programming language, focusing on PHP version 4.0.4 for the most part. The information is provided on an as-is basis, and no warranty or fitness is implied. All persons and entities shall have neither liability nor responsibility to any person or entity with respect to any loss or damage arising from the information contained in this book.

Powered by: vBulletin Version 3.0.7
Copyright ©2000 - 2005, Jelsoft Enterprises Ltd.
Copyright © 2001 - 2005, ThinkDing LLC. All Rights Reserved.