Home  >  Article  >  php教程  >  The difference between time() and mktime() methods in php_php basics

The difference between time() and mktime() methods in php_php basics

WBOY
WBOYOriginal
2016-05-16 09:00:082397browse

The time() function returns the current time. The main function of the mktime() function is not to return the current time, but to format the time. Although writing mktime() alone without any parameters such as echo mktime() and echo time() has the same effect. But it's essentially different.

PHP mktime() function

PHP Date / Time function

Definition and usage

The mktime() function returns the Unix timestamp of a date.
The argument always represents a GMT date, so is_dst has no effect on the result.
The parameters can be left empty in order from right to left, and the empty parameters will be set to the corresponding current GMT value.

Grammar

mktime(hour,minute,second,month,day,year,is_dst)
Parameter Description
hour Optional. Specified hours.
minute is optional. Specified minutes.
second is optional. Specifies seconds.
month Optional. Specifies the numeric month.
day Optional. Specify days.
year Optional. Specified year. On some systems, legal values ​​are between 1901 - 2038. However, this limitation no longer exists in PHP 5.
is_dst

Optional. Set to 1 if the time is during Daylight Saving Time (DST), 0 otherwise, or -1 if unknown.
As of 5.1.0, the is_dst parameter is deprecated. Therefore the new time zone handling features should be used.

Tips and Notes

Note: Before PHP 5.1, if the parameter of this function is illegal, it will return false.
Example
The mktime() function is very useful for date operations and verification. It can automatically correct out-of-bounds input:

Copy code The code is as follows:

echo(date(" M-d-Y",mktime(0,0,0,12,36,2001)));
echo(date("M-d-Y",mktime(0,0,0,14,1,2001)));
echo(date("M-d-Y",mktime(0,0,0,1,1,2001)));
echo(date("M-d-Y",mktime(0,0,0,1,1,99 )));
?>

Output:
Jan-05-2002
Feb-01-2002
Jan-01-2001
Jan-01-1999
PHP time() function
PHP Date /Time function

time() definition and usage

The time() function returns the Unix timestamp of the current time.

Grammar

time(void)
Parameter Description
void Optional.
Description
Returns the number of seconds since the Unix epoch (January 1, 1970 00:00:00 GMT) to the current time.

Tips and Notes

Tip: Since PHP 5.1, the timestamp of the time when the request was initiated is saved in $_SERVER['REQUEST_TIME'].

Example

Example 1

Copy code The code is as follows:

$t=time();
echo($t . "
");
echo(date("D F d Y",$t));
?>

输出:

1138618081
Mon January 30 2006

例子 2

复制代码 代码如下:

$nextWeek = time() (7 * 24 * 60 * 60); // 7 days; 24 hours; 60 mins; 60secs
echo 'Now:       '. date('Y-m-d') ."\n";
echo 'Next Week: '. date('Y-m-d', $nextWeek) ."\n";
?>

输出:
Now:       2005-03-30
Next Week: 2005-04-07

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn