Home  >  Article  >  Backend Development  >  Analysis of the difference between time() and mktime() usage in php

Analysis of the difference between time() and mktime() usage in php

WBOY
WBOYOriginal
2016-07-25 08:58:241009browse
This article introduces the difference in usage between time function time() and mktime() in PHP. Friends in need may refer to it.

In PHP programming, the time function time() returns the current time. The mktime() function does not return the current time, but the formatted 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 functions

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 is optional. Specifies the numeric month. day is optional. Specify days. year is 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.

Since 5.1.0, the is_dst parameter is deprecated. Therefore the new time zone handling features should be used. Tips and Notes Note: Prior to PHP 5.1, this function would return false if its argument was illegal.

Example: The mktime() function is useful for date operations and validation. It can automatically correct out-of-bounds input:

<?php
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 functions

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

Grammar time(void)

Parameter Description void Optional. illustrate 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 1,

<?php
//time()函数示例1
$t=time();
echo($t . "<br />");
echo(date("D F d Y",$t));
?>

Output: 1138618081 Mon January 30 2006

Example 2,

<?php
//time()函数示例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";
?>

Output: Now: 2005-03-30 Next Week: 2005-04-07

>>>> Articles you may be interested in: Detailed explanation of the differences between time(), date(), and mktime() in php Y2K38 vulnerability of strtotime() and mktime() in php 2038 issue Analysis of examples of using mktime in php to obtain timestamps



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