Differences between time () and mktime () methods in php _ php Basics-php Manual

Source: Internet
Author: User
Tags php basics
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 () function to return the current time. The main function of the mktime () function is not to return the current time, but to format the time. Although mktime () is written separately without adding any parameters such as echo mktime () and echo time (), the effect is the same. But they are essentially different.

PHP mktime () function

PHP Date/Time function

Definition and usage

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

Syntax

Mktime (hour, minute, second, month, day, year, is_dst)
Parameter description
Hour is optional. The specified hour.
Minute is optional. Minutes.
Second is optional. Specified seconds.
Month is optional. Indicates the month in number.
Day is optional. Specified day.
Year is optional. Specified year. In some systems, the valid value ranges from 1901 to 2038. However, this restriction does not exist in PHP 5.
Is_dst

Optional. If the time is in the daylight saving time (DST) period, it is set to 1; otherwise, it is set to 0. if it is unknown, it is set to-1.
The is_dst parameter has been deprecated since 5.1.0. Therefore, the new time zone processing feature should be used.

Tips and comments

Note: Before PHP 5.1, if the parameter of this function is invalid, false is returned.
Example
The mktime () function is very useful for date calculation and verification. It can automatically correct out-of-range input:

The code is as follows:


Echo (date ("M-d-Y", mktime )));
Echo (date ("M-d-Y", mktime )));
Echo (date ("M-d-Y", mktime )));
Echo (date ("M-d-Y", mktime )));
?>

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.

Syntax

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

Tips and comments

Tip: Since PHP 5.1, the timestamp at which the REQUEST is initiated is saved in $ _ SERVER ['request _ time.

Example

Example 1

The code is as follows:


$ T = time ();
Echo ($ t ."
");
Echo (date ("d f d Y", $ t ));
?>

Output:

1138618081
Mon January 30 2006

Example 2

The code is as follows:


$ NextWeek = time () + (7*24*60*60); // 7 days; 24 hours; 60 mins; 60 secs
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.