Home >Backend Development >PHP Tutorial >PHP Notes: Introduction to the use of date functions_PHP Tutorial
Introduction
PHP is a very amazing language. It is powerful enough (the core language of the largest blog (WordPress)), it is widespread enough (runs on Facebook, the largest social networking site), and it is simple enough (as the preferred introductory language for beginners). Works well on low-cost machines. Moreover, the PHP language has many very good server packages (such as WAMP and MAMP), which are very convenient to install on your machine. PHP has very rich library resources, making it easy for developers to handle some businesses. Since we have the most contact with dates in the project, we will start learning from the date function today.
Give a simple date example
I will use the echo command to output the content to our client (browser). I will use the following code as the base code.
Today is Friday This function outputs the text format of the day of the week. The date function requires at least one character parameter (this parameter tells us how to format the current date).
Try different formats
If you look at the PHP date function in the php manual, you will find that there are many ways to format dates.
will get
Today is 2012-08-17
Some dates are commonly used, so PHP provides some constants for you to use. For example, you can use cookies to get the client date.
You will get the following content
Today is Friday, 17-Aug-12 11:34:38 CST Be careful not to use quotation marks when using constants.
What time is it now?
If you want to output the current time, you can use date (different formatting character parameters).
You will get
The time is 11:39:59am
Localize your time zone
If you find that the above code does not give the correct time, it is most likely because your server is set to a different time zone than your local time zone. You need to specify the time zone on the server, then you use the following code:
This will set the Shanghai time zone in China. This is a function of php5 (note older versions of php), there are many for you to choose the time zone. If you want it to be permanent, you can modify your php.ini file.
Get other times
You often need other times, not the current time. When you create a time using the date() function, the system uses Unix system time. This time represents the number of seconds since January 1, 1970 00:00:00 GMT (Unix Epoch Time) until now.
To specify how to get the date at a specified time, you can provide the number of seconds as the second parameter of the date(0 function.
The result is:
Today is 2011-06-27
This may seem useless, but it means you can use the date() function to do calculations. Before doing this, you need to simply create a timestamp.
Creating timestamps
There are many ways to create timestamps. We can use the mktime() function to get the timestamp we need.
Today is 2011-06-26 9:23:33am mktime()
The function requires you to pass hours, minutes, seconds, months, days, and years in sequence. This is a good way to get a timestamp, but there are cooler ways.
Get the timestamp through characters
You can use the strtotime() function to get the timestamp, and PHP converts readable characters into Unix timestamps. PHP is quite flexible in converting characters into timestamps, so you can plug in all kinds of values to get the timestamp you want.
This is a simple example:
Today is 2011-06-26 7:50:00pm
PHP is pretty slick at interpreting characters, but it's not perfect, so be sure to test the characters you enter before you insert them. Use "english-like instructions" to convert the required timestamp, which is a very good way. You can do it like this:
Number of days to a certain date
When using the calculator, we will try to calculate the number of days to a certain day. You can easily calculate the timestamp of the fourth Thursday in November.