The difference between time () and Mktime () methods in PHP _php tutorial

Source: Internet
Author: User
The time () function returns the current date. The main function of the mktime () function is not to return the current time, but rather to format the time. Although writing mktime () alone does not add any parameters such as Echo mktime () and Echo time (), the effect is the same. But it's not the same in nature.

PHP mktime () function

PHP Date/time functions

Definition and usage

The Mktime () function returns a Unix timestamp for a date.
The parameter always represents the GMT date, so IS_DST has no effect on the result.
Parameters can be left-to-right and empty, and empty parameters will be set to the corresponding current GMT value.

Grammar

Mktime (HOUR,MINUTE,SECOND,MONTH,DAY,YEAR,IS_DST)
Parameter description
Hour is optional. Specified hours.
Minute is optional. Specify minutes.
Second is optional. Specify seconds.
Month is optional. Specifies the number of months to be represented.
Day is optional. Prescribed days.
Year is optional. Prescribed year. On some systems, the legal value is between 1901-2038. However, there is no such limit in PHP 5.
Is_dst

Optional. If the time is in daylight saving time (DST), set to 1, otherwise set to 0 and if unknown, set to-1.
Since 5.1.0, the IS_DST parameter has been discarded. Therefore, you should use the new Time zone processing feature.

Hints and Notes

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

Copy the Code code 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 functions

Time () Definition and usage

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

Grammar

Time (void)
Parameter description
void is optional.
Description
Returns the number of seconds since the Unix era (January 1, 1970 00:00:00 GMT) to the current time.

Hints and Notes

Tip: From PHP 5.1, the timestamp of the time the request was initiated was saved in $_server[' Request_time '.

Example

Example 1

Copy the Code code as follows:
$t =time ();
Echo ($t. "
");
Echo (Date ("D F D Y", $t));
?>

Output:

1138618081
Mon January 30 2006

Example 2

Copy the Code code as follows:
$nextWeek = time () + (7 * 24 * 60 * 60); 7 days; Hours; 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

http://www.bkjia.com/PHPjc/313553.html www.bkjia.com true http://www.bkjia.com/PHPjc/313553.html techarticle the time () function returns the current date. The main function of the mktime () function is not to return the current time, but rather to format the time. Although write Mktime () alone without any parameters such as: Echo Mkt ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.