Difference between time (), date () and mktime () in PHP _ php Basics-php Manual

Source: Internet
Author: User
Tags echo date month name php basics
PHP: time (), date (), mktime () date and time function libraries are often forgotten. here, you can add them to your favorites for convenience: checkdate: to verify the correctness of the date.
Date: format the server time.
Strftime: format the server time locally.
Getdate: get the time and date information.
Gettimeofday: get the current time.
Gmdate: get the current time difference from GMT.
Easter_date: calculate the Easter date.
Easter_days: calculates the number of days between Easter and January 1, lunar March 21.
Mktime: obtain UNIX timestamps.
Gmmktime: returns the Greenwich mean time of UNIX timestamps.
Time: obtain the UNIX timestamp of the current time.
Microtime: Gets the UNIX timestamp value of one second per million at the current time.

Checkdate verifies the correctness of the date.

Syntax: int checkdate (int month, int day, int year );
Return value: integer
Function type: time and date
If the date is valid, true is returned. if the date is incorrect, false is returned. This function can be used to check whether the date is valid. The valid range is as follows:
The year is from 0 to 32767 years.
The month is from 1 to December.
The day changes with the month and the leap year.

Date format the server time.

Syntax: string date (string format, int [timestamp]);
Return value: string
Function type: time and date
Description of returned values? The string is determined by the configured format. If a timestamp value is input, the timestamp is formatted and returned. If no timestamp value is input, the current server time is formatted and returned. To convert a date to another language format, use the setlocale () and strftime () functions. The options for string formatting are as follows:
A-"am" or "pm"
A-"AM" or "PM"
D-a few days, two digits. if there are less than two digits, fill in the first zero. for example, "01" to "31"
D-the day of the week, with three English letters, for example, "Fri"
F-month, full English name; for example: "January"
H-12 hours, for example, "01" to "12"
H-24 hours, for example, "00" to "23"
Hours in the g-12 hour format. less than two hours do not fill in zero. for example: "1" to 12"
Hours in the G-24-hour format. less than two hours do not fill in zero. for example: "0" to "23"
I-minutes; for example: "00" to "59"
J-a few days, two digits. if less than two digits are left blank, for example, "1" to "31"
L-the day of the week, full name in English; for example: "Friday"
M-month, two digits. if there are less than two digits, add zero in front, for example, "01" to "12"
N-month, two digits. if less than two digits are left blank, for example, "1" to "12"
M-month, with three English letters, for example, "Jan"
S-seconds; for example: "00" to "59"
The end of the S-character is followed by an English sequence, with two English letters, for example, "th", "nd"
T-specifies the number of days in a month, for example, "28" to "31"
U-total seconds
W-number of weeks, for example, "0" (Sunday) to "6" (Saturday)
Y-year, four digits, such as: "1999"
Y-year, two digits, for example, "99"
Z-the day of the year, for example, "0" to "365"
Other characters not in the upper column are listed directly.

Example,

Example 1:

The code is as follows:


Print (date ("l dS of f y h: I: s "));
Print ("July 1, 2000 is on a". date ("l", mktime )));
?>

Example 2:

The code is as follows:


$ Tomorrow = mktime (0, 0, 0, date ("m"), date ("d") + 1, date ("Y "));
$ Lastmonth = mktime (0, 0, date ("m")-1, date ("d"), date ("Y "));
$ Nextyear = mktime (0, 0, 0, date ("m"), date ("d", date ("Y") + 1 );
?>

See gmdate () mktime ()
Strftime format the server time locally.
Syntax: string strftime (string format, int [timestamp]);
Return value: string
Function type: time and date
The content indicates that the returned value string is determined by the configuration format. If a timestamp value is input, the timestamp is formatted and returned. If no timestamp value is input, the current server time is formatted locally. The name of a month or week varies with the settings of setlocale () in the local language.
The returned string can be in the following format:
% A abbreviation of the day of the week.
% A full name of the day of the week.
The abbreviation of % B month name.
% B full name of the month name.
% C the local date time is better than the string.
% D indicates the day of the month (range: 00 to 31) with a number ).
% H indicates the hour in the 24-hour format (ranging from 00 to 23 ).
% I represents the hour in 12-hour format (range: 01 to 12 ).
% J indicates the day of the year (range: 001 to 366 ).
The number of % m months (ranging from 1 to 12 ).
% M minutes.
% P uses 'am' or 'PM 'to indicate the local time.
% S seconds.
% U indicates the week of the current year. The first week starts from the first Sunday.
% W indicates the week of the current year. The first week starts from the first Monday.
% W indicates the day of the week by number (0 indicates Sunday ).
% X date representation without time.
% X does not include the time representation of the date.
The two digits % y indicate the year (range: 00 to 99 ).
% Y indicates the complete year number, that is, the four-digit number.
% Z time zone or abbreviated name.
% Characters.

Example

The code is as follows:


Setlocale ("LC_TIME", "C ");
Print (strftime ("% A in Finnish is "));
Setlocale ("LC_TIME", "fi ");
Print (strftime ("% A, in French "));
Setlocale ("LC_TIME", "fr ");
Print (strftime ("% A and in German "));
Setlocale ("LC_TIME", "de ");
Print (strftime ("% A. n "));
?>

Refer to setlocale () mktime ()
Getdate obtains the time and date information.
Syntax: array getdate (int timestamp );
Returned value: Array
Function type: time and date
The returned array elements include the following items:
"Seconds"-seconds
"Minutes"-minute
"Hours"-Hour
"Mday"-the day of the month
"Wday"-number of the day of the week
"Mon"-number of months
"Year"-year, number
"Yday"-the number of the day of the current year, for example, "299"
"Weekday"-full name of the day of the week, for example, "Friday"
"Month"-full name of the month, for example, "January"
Gettimeofday gets the current time.
Syntax: array gettimeofday (void );
Returned value: Array
Function type: time and date
The returned array elements include the following items:
"Sec"-seconds
"Usec"-one minute per second
"Minuteswest"-Greenwich Mean Time score
"Dsttime"-destination time zone
Gmdate returns the time difference between the current time and GMT.
Syntax: string gmdate (string format, int timestamp );
Return value: string
Function type: time and date
This function is similar to the date () function. The difference is that this function returns the Time after the Time difference from the Greenwich Mean Time (GMT ).

Example

The code is as follows:


Echo date ("M d y h: I: s", mktime ));
Echo gmdate ("M d y h: I: s", mktime ));
?>

If the machine running this example is in Finland (Finland, GMT + 0200), the returned result is:
Jan 01 00:00:00 1998
Dec 31 1997 22:00:00
Refer to date () mktime () gmmktime ()
Easter_date is used to calculate the Easter date.
Syntax: int easter_date (int [year]);
Return value: integer
Function type: time and date
Description: If a year is input, the Easter date of the year is returned in UNIX time stamp format. if the year is not input, the date of the year is calculated. Value? Note that the input year must be between 1970 a.m. and 2037 a.m.; otherwise, it cannot be calculated.
Example

The code is as follows:


Echo date ("M-d-Y", easter_date (1999 ));
Echo date ("M-d-Y", easter_date (2000 ));
Echo date ("M-d-Y", easter_date (2001 ));
?>

The returned result is

Apr-04-1999
Apr-23-2000
Apr-15-2001
Easter_days is used to calculate the number of days between Easter and January 1, lunar March 21.

Syntax: int easter_days (int [year]);
Return value: integer
Function type: time and date
Description: If a year is input, the number of days between Easter and January 1, March 21 is calculated. if the year is not input, the current year is used. This function can be used to replace the issue that cannot be calculated beyond the range easter_date () 1970-2037.
Example

The code is as follows:


Echo easter_days (1999 );
Echo easter_days (1492 );
Echo easter_days (1913 );
?>

The returned result is:
14 (4/4)
32 (4/22)
2 (3/23)
Refer to easter_date ()
Mktime gets UNIX timestamps.
Syntax: int mktime (int hour, int minute, int second, int month, int day, int year );
Return value: integer
Function type: time and date
Description: enter a time to return a long integer of UNIX timestamps.
Example

The code is as follows:


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

Refer to date () time ()

Gmmktime obtains the Greenwich mean time of UNIX timestamps.

Syntax: int gmmktime (int hour, int minute, int second, int month, int day, int year );

Return value: integer

Function type: time and date
Description: enter a time to return a long integer of the UNIX Greenwich mean time stamp.
Time gets the UNIX timestamp of the current time.
Syntax: int time (void );
Return value: integer
Function type: time and date
The content description returns the stamp value of the current time.

Refer to date ()

Microtime gets the UNIX timestamp value of one second per million at the current time.
Syntax: string microtime (void );
Return value: string
Function type: time and date
Description: returns the timestamp value of one million points per second of the current time. If the operating system does not provide the gettimeofday () system call function, this function is also invalid.

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.