Home php教程 php手册 Introduction to the differences between time(), date(), and mktime() in PHP_php basics

Introduction to the differences between time(), date(), and mktime() in PHP_php basics

May 16, 2016 am 09:00 AM
date mktime time

checkdate: Verify the correctness of the date.
date: Format the server's time.
strftime: Format the server's time locally.
getdate: Get time and date information.
gettimeofday: Get the current time.
gmdate: Get the time difference between the current time and GMT.
easter_date: Calculate Easter date.
easter_days: Calculate the number of days between Easter and March 21st.
mktime: Get UNIX timestamp.
gmmktime: Get the Greenwich Mean Time of a UNIX timestamp.
time: Get the UNIX timestamp of the current time.
microtime: Gets the UNIX timestamp of the current time in millionths of a second.

Checkdate Verifies 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:
Year is from 0 to 32767
Month is from 1 to December
The day will change 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 less than two digits Then 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; e.g.: "01" to "12"
H - hour in 24-hour format; e.g.: "00" to "23"
g - hour in 12-hour format, less than Two digits are not padded with zeros; for example: "1" to 12"
G - hour in 24-hour system, less than two digits are not padded with zeros; for example: "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, two digits, if there are less than two digits, add zeros in front; For example: "01" to "12"
n - month, two digits, if there are less than two digits, no Fill with zeros; such as: "1" to "12"
M - month, three English letters; such as: "Jan"
s - seconds; 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 - numeric day of the week, such as: "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; such as: "0" to "365"
Other characters not listed above will be listed directly.

Usage examples,

Example 1:

Copy code The code is as follows:


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:

Copy code The code is as follows:


$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 Abbreviation of 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 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 001 to 366).
%m Month number (range 1 to 12).
%M minutes.
%p represents local time in 'AM' or 'PM'.
%S seconds.
The %U number represents the week number of the year, with the first week starting on the first Sunday.
%W The number represents the week number of the year, with the first week starting on 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 is a two-digit number representing the year (ranging from 00 to 99).
%Y The complete year numeric representation, that is, four digits.
%Z time zone or name abbreviation.
%% % characters.

Usage examples

Copy code 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"));
?>
div>

Refer to setlocale() mktime()
getdate to obtain time and date information.
Syntax: array getdate(int timestamp);
Return value: Array
Function type: Time 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 number
"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 Get the current time.
Syntax: array gettimeofday(void);
Return value: Array
Function type: Time date
Content description The elements of the returned array include the following items:
"sec" - seconds
"usec" - one millionth of a second
"minuteswest" - minutes of Greenwich Mean Time
"dsttime" - the destination time zone
gmdate Gets 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 examples

Copy code The code is as follows:

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 result is:
Jan 01 1998 00:00:00
Dec 31 1997 22:00:00
Reference date () mktime() gmmktime()
easter_date Calculate the Easter date.
Syntax: int easter_date(int [year]);
Return value: Integer
Function type: Time date
Content description: If you enter a certain year, 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

Copy code 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 return result is

Apr-04-1999
Apr-23-2000
Apr-15-2001
easter_days Counts 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 examples

Copy code The code is as follows:


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

The return result is:
14 (4/4)
32 (4/22)
2 (3/23)
Refer to easter_date()
mktime to obtain the UNIX timestamp.
Syntax: int mktime(int hour, int minute, int second, int month, int day, int year);
Return value: Integer
Function type: Time and date
Content description: Enter one time, returns a UNIX timestamp long integer.
Usage examples

Copy code The code is as follows:


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 and date
Content description: If you enter a time, a long integer of the UNIX Greenwich time stamp will be returned.
time Gets the UNIX timestamp of the current time.
Syntax: int time(void);
Return value: Integer
Function type: Time and date
Content description Return the stamp value of the current time.

Reference date()

microtime Gets the millionth of a second value of the UNIX timestamp of the current time.
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.

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP Warning: date() expects parameter 2 to be long, string given solution PHP Warning: date() expects parameter 2 to be long, string given solution Jun 22, 2023 pm 08:03 PM

When developing using PHP programs, you often encounter some warning or error messages. Among them, one error message that may appear is: PHPWarning:date()expectsparameter2tobelong,stringgiven. The error message means: the second parameter of the function date() is expected to be a long integer (long), but what is actually passed to it is a string (string). So, we

How to create and name a file/folder based on current timestamp How to create and name a file/folder based on current timestamp Apr 27, 2023 pm 11:07 PM

If you're looking for a way to automatically create and name files and folders based on system timestamps, you've come to the right place. There is a super simple way to accomplish this task. The created folders or files can then be used for various purposes such as storing file backups, sorting files based on date, etc. In this article, we will explain in some very simple steps how to automatically create files and folders in Windows 11/10 and name them according to the system’s timestamp. The method used is a batch script, which is very simple. Hope you enjoyed reading this article. Section 1: How to automatically create and name a folder based on the current timestamp of the system Step 1: First, navigate to the parent folder where you want to create the folder,

Monotonic clock processing of time package Monotonic clock processing of time package Aug 04, 2023 pm 05:45 PM

Today we are mainly going to take a look at the time application method of golang time package. The general rule between the two is that "wall time" is used to tell time, and "monotonic clock" is used to measure time; there are other clock processing methods.

How to get the millisecond representation of a date using the getTime() method of the Date class How to get the millisecond representation of a date using the getTime() method of the Date class Jul 24, 2023 am 11:42 AM

How to get millisecond representation of date using getTime() method of Date class In Java, Date class is a class used to represent date and time. It provides many useful methods to manipulate and obtain information about date objects. Among them, the getTime() method is an important method in the Date class, which can return the millisecond representation of the date object. Next, we will detail how to use this method to obtain the millisecond representation of a date, and provide corresponding code examples. Using the Date class

Introduction to methods and usage of using Date and SimpleDateFormat classes to process time in Java Introduction to methods and usage of using Date and SimpleDateFormat classes to process time in Java Apr 21, 2023 pm 03:01 PM

1. Introduction The Date class in the java.util package represents a specific time, accurate to milliseconds. If we want to use our Date class, then we must introduce our Date class. Writing the year directly into the Date class will not produce the correct result. Because Date in Java is calculated from 1900, so as long as you fill in the first parameter with the number of years since 1900, you will get the year you want. The month needs to be subtracted by 1, and the day can be inserted directly. This method is rarely used, and the second method is commonly used. This method is to convert a string that conforms to a specific format, such as yyyy-MM-dd, into Date type data. First, define an object of Date type Date

What are the options for calendar and date libraries in Python? What are the options for calendar and date libraries in Python? Oct 21, 2023 am 09:22 AM

There are many excellent calendar libraries and date libraries in Python for us to use. These libraries can help us handle date and calendar related operations. Next, I will introduce you to several common choices and provide corresponding code examples. Datetime library: Datetime is Python's built-in date and time processing module. It provides many date and time related classes and methods, which can be used to process dates, times, time differences and other operations. Sample code: importdatetime#Get the current date

How to use Stringbuild, Date and Calendar classes in Java How to use Stringbuild, Date and Calendar classes in Java May 22, 2023 pm 04:52 PM

Stringbuild class Since the object content of the String class cannot be changed, a new String object will be constructed every time it is spliced, which is time-consuming and wastes memory space. At this time, you need to solve this problem through the StringBuild class provided by Java. StringBuilder is also called a variable character sequence. , it is a string buffer similar to String, which can be regarded as a container. Many strings can be held in the container. Variable means that the content in the StringBuilder object is variable. The construction method publicStringBuilder(): creates an empty buffer publicStringBuilder(Stringsr

How to solve the problem of springboot configuring date field to return timestamp How to solve the problem of springboot configuring date field to return timestamp May 20, 2023 am 11:16 AM

I encountered a problem. After springboot was upgraded to 2.0, the date found from the database was received using Date and finally returned directly to the front end. It can be displayed normally in the yyyy-MM-ddHH:mm:ss format in Google Chrome. However, the date displayed in the IE browser is "garbled" because the springboot1.x version returns a timestamp in the Date field by default, and Google and IE will automatically convert the timestamp into yyyy-MM-ddHH:mm:ss ;After springboot2.0, spring will automatically convert the Date field into a UTC string (without configuration), so date needs to be converted into a timestamp or y

See all articles