News Tutorials Projects Reviews Authors Contact Search Links Admin
 

Date and Time : Time and Mktime

time

(PHP 3, PHP 4 )

time -- Return current UNIX timestamp

Description

int time (void)

Returns the current time measured in the number of seconds since the Unix Epoch (January 1 1970 00:00:00 GMT).


mktime

(PHP 3, PHP 4 )

mktime -- Get UNIX timestamp for a date

Description

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

Returns the Unix timestamp corresponding to the arguments given. This timestamp is a long integer containing the number of seconds between the Unix Epoch (January 1 1970) and the time specified.

This 'time' can then be used in conjunction with the date() function to output a date string based on a give time.

Code Examples

mktime() is useful for doing date arithmetic and validation, as it will automatically calculate the correct value for out-of-range input. For example, each of the following lines produces the string "Jan-01-1998".

echo date ("M-d-Y", mktime (0,0,0,12,32,1997));
echo date ("M-d-Y", mktime (0,0,0,13,1,1997));
echo date ("M-d-Y", mktime (0,0,0,1,1,1998));
echo date ("M-d-Y", mktime (0,0,0,1,1,98));

The last day of any given month can be expressed as the "0" day of the next month, not the -1 day. Both of the following examples will produce the string "The last day in Feb 2000 is: 29".

$lastday = mktime (0,0,0,3,0,2000);
echo strftime ("Last day in Feb 2000 is: %d", $lastday);

$lastday = mktime (0,0,0,4,-31,2000);
echo strftime ("Last day in Feb 2000 is: %d", $lastday);

 


Author: Markavian
Last edited on: 8th Dec, 12:50 am
Please rate this article.

« Prev page 'Date and Strftime' « » Next Page 'MySQL Data and Time' »

tutorial Pages