Home >Backend Development >PHP Tutorial >PHP -time(), date(), mktime() date and time function library_PHP tutorial
checkdate Verify the correctness of the date.
Syntax: int checkdate(int month, int day, int year);
Return value: Integer
Function type: Time and date
Content description If the date is valid, return true, if the date has problem, return false. This function can be used to check whether the date is valid. The valid range is as follows:
The year ranges from 0 to 32767
The month ranges from 1 to December
The day changes with the month and leap year
Date: Format the server's time.
Syntax: string date(string format, int [timestamp]);
Return value: String
Function type: Time date
Content description The string of the return value is determined by the configured format . If there is a timestamp value passed in, the timestamp will be formatted and returned; if there is no timestamp value passed in, the time of the current server will be formatted and returned. To convert dates to other language formats, the setlocale() and strftime() functions should be used. The options for string formatting are as follows:
a - "am" or "pm"
A - "AM" or "PM"
d - day, two digits, if there are less than two digits, add zeros in front; such as: "01" to "31"
D - day of the week, three English letters; such as: "Fri"
F - month, full English name; such as: "January"
h - hour in 12-hour format; such as: "01" to "12"
H - hour in 24-hour format; such as: "00" to "23"
g - hour in 12-hour format, no zeros are added for less than two digits; such as: "1" to 12"
G - hour in 24-hour format, no zeros are added for less than two digits; such as: "0" to "23"
i - minutes; such as: "00" to "59"
j - Day, two digits, if there are less than two digits, do not add zero; such as: "1" to "31"
l - Day of the week, full English name; such as: "Friday"
m - Month, Tuesday A digit number, if there are less than two digits, add zeros in front; such as: "01" to "12"
n - month, a two-digit number, if there are less than two digits, do not add zeros; such as: "1" to "12" "
M - month, three English letters; such as: "Jan"
s - second; such as: "00" to "59"
S - add an English ordinal number at the end of the word, two English letters; Such as: "th", "nd"
t - The number of days in the specified month; such as: "28" to "31"
U - The total number of seconds
w - The numeric day of the week, such as: " 0" (Sunday) to "6" (Saturday)
Y - year, four digits; such as: "1999"
y - year, two digits; such as: "99"
z - one The day of the year; such as: "0" to "365"
Other characters not listed above will be listed directly.
Usage example, Example 1:
print(date( "l dS of F Y h:i:s A" ));
print("July 1, 2000 is on a " . date ("l", mktime(0,0,0,7,1,2000)));
?>
Example 2:
$tomorrow = mktime (0,0,0,date("m") ,date("d")+1,date("Y"));
$lastmonth = mktime(0,0,0,date("m" )-1,date("d"), date("Y"));
$nextyear = mktime(0,0,0,date("m"), date("d", date("Y ")+1);
?>
Reference gmdate() mktime()
strftime Format the server's time locally.
Syntax: string strftime(string format, int [timestamp]);
Return value: String
Function type: Time and date
Content description The string of the return value is determined by the configured format. If there is a timestamp value passed in, the timestamp will be formatted and returned; if there is no timestamp value passed in, the time of the current server will be formatted locally and returned. The month or day of the week name changes depending on the locale configuration setlocale().
The returned string can be in the following format:
%a The abbreviation of the day of the week.
%A The full name of the day of the week.
%b The abbreviation of the month name.
%B The full name of the month.
%c is a string representing the local date and time better.
%d represents the day of the month as a number (range is 00 to 31).
%H Represents the hour as a 24-hour number (range 00 to 23).
%I Represents the hour as a 12-hour number (range 01 to 12).
%j represents the day of the year as a number (range is 001 to 366).
%m Month number (ranging from 1 to 12).
%M minutes.
%p represents the local time in 'AM' or 'PM'.
%S seconds.
%U The number represents the week number of the year, and the first week starts from the first Sunday.
%W The number represents the week number of the year, and the first week starts with the first Monday.
%w represents the day of the week as a number (0 is Sunday).
%x Date representation without time.
%X Time representation without date.
%y A two-digit number representing the year (range from 00 to 99).
%Y is the complete numerical representation of the year, that is, four digits.
%Z time zone or name abbreviation.
%% % characters.
Usage example
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: Get time and date information.
Syntax: array getdate(int timestamp);
Return value: Array
Function type: Time and date
Content description The elements of the returned array include the following items:
"seconds" - seconds
"minutes" - minutes
"hours" - hours
"mday" - the day of the month
"wday" - the day of the week
"mon" - the month
"year" - year, number
"yday" - the day of the year; such as: "299"
"weekday" - the full name of the day of the week; such as: "Friday"
" month" - the full name of the month; such as: "January"
Gettimeofday Gets the current time.
Syntax: array gettimeofday(void);
Return value: Array
Function type: Time and date
Content description The elements of the returned array include the following items:
"sec" - seconds
"usec" - millionths of a second
"minuteswest" - minutes of Greenwich Mean Time
"dsttime" - destination time zone
gmdate: Get the time difference between the current time and GMT.
Syntax: string gmdate(string format, int timestamp);
Return value: String
Function type: Time date
Content description: This function is similar to the date() function, except that this function Returns the time difference from Greenwich Mean Time (GMT)
Usage example
echo date( "M d Y H:i:s",mktime(0,0,0,1,1,1998) );
echo gmdate( "M d Y H:i:s",mktime(0,0,0,1,1,1998) );
?>
If the machine executing this example is in Finland (Finland, GMT +0200), the returned The result is:
Jan 01 1998 00:00:00
Dec 31 1997 22:00:00
Reference date() mktime() gmmktime()
Easter_date Calculates Easter date.
Syntax: int easter_date(int [year]);
Return value: Integer
Function type: Time date
Content description: Enter a certain year, and the year will be returned in UNIX timestamp format. The Easter date of , if no year is entered, the date of the current year is calculated. Value? Note that the entered year must be between 1970 and 2037 AD, otherwise it cannot be calculated.
Usage examples
echo date("M-d-Y", easter_date(1999));
easter_date(2001));
?>
The returned result is
Apr-04-1999
Apr-23-2000
Apr-15-2001
easter_days Calculate the number of days between Easter and March 21st.
Syntax: int easter_days(int [year]);
Return value: Integer
Function type: Time and date
Content description Enter a certain year to calculate Easter and March 2 of that year The number of dates between eleven days. If no year is entered, it will be calculated based on the current year. This function can be used to replace the problem that easter_date() cannot calculate outside the range of 1970-2037.
Usage example
echo easter_days(1999);
echo easter_days(1492);
> 14 (4/4)
32 (4/22)
2 (3/23)
Refer to easter_date()
mktime Gets UNIX timestamp.
Syntax: int mktime(int hour, int minute, int second, int month, int day, int year);
Return value: Integer
Function type: Time date
Content description: Enter one time, returns a UNIX timestamp long integer.
Usage example
echo date( "M-d-Y", mktime(0,0,0,12,32,1997) );
echo date( "M-d-Y", mktime(0,0,0,13,1,1997) );
echo date( "M-d-Y", mktime(0,0,0,1,1,1998) );
?>
Reference date() time()
gmmktime Gets the Greenwich Mean Time of a UNIX timestamp.
Syntax: int gmmktime(int hour, int minute, int second, int month, int day, int year);
Return value: Integer
Function type: Time date
Content description: Enter a time, returns a UNIX Greenwich Timestamp long integer.
time: Get the UNIX timestamp of the current time.
Syntax: int time(void);
Return value: Integer
Function type: Time and date
Content description Returns the stamp value of the current time.
Reference date()
microtime: Gets the UNIX timestamp of the current time in millionths of a second.
Syntax: string microtime(void);
Return value: String
Function type: Time and date
Content description Returns the millionth of a second stamp value of the current time. If the operating system does not provide the system call function of gettimeofday(), this function will also be invalid.