The difference between time () and Mktime () method in PHP _php base

Source: Internet
Author: User
Tags comments current time

The time () function returns the current period. The main function of the mktime () function is not to return the current time, but to format the time. Although the effect of writing mktime () without any parameters such as Echo mktime () and Echo time () is the same. But it's not the same in nature.

PHP mktime () function

PHP date/time function

Definitions 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.
The parameters can be left-to-right, and the empty arguments will be set to the appropriate current GMT value.

Grammar

Mktime (HOUR,MINUTE,SECOND,MONTH,DAY,YEAR,IS_DST)
Parameter description
Hour optional. Set hours.
Minute optional. Specify minutes.
Second Optional. Specified seconds.
Month optional. A month that is specified in numbers.
Day is optional. Stipulated day.
Year is optional. Set year. On some systems, the legal value is between 1901-2038. However, there is no such limit in PHP 5.
Is_dst

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

Tips and comments

Note: Before PHP 5.1, false is returned if the function's arguments are illegal.
Example
The Mktime () function is useful for date operations and validation. It can automatically correct the input that crosses the line:

Copy Code code as follows:

<?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 function

Time () Definition and usage

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

Grammar

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

Tips and comments

Tip: The timestamp that originated at the time of the request was saved from PHP 5.1 in $_server[' Request_time '.

Example

Example 1

Copy Code code as follows:

<?php
$t =time ();
Echo ($t. "<br/>");
Echo (Date ("D F D Y", $t));
?>

Output:

1138618081
Mon January 30 2006

Example 2

Copy Code code as follows:

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

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.