I am planning to write a help processing system in PHP. I found that I had to know how long it took to handle the last customer's problem? When I used ASP in the past, it was quite simple to solve this problem. The corresponding function DateDiff of ASP can be used to show how many months, how many days, and how many seconds are separated between two days. After searching for the PHP manual, I found that PHP does not have similar functions.
This article includes the following content:
1. Get the current date and time-how many methods do we have?
2. Change the date display mode-date and time display form
3. Convert the current date to the Unix timestamp value
4. Change date
A. Add time
B. Minus the time
C. Find the interval between two dates
5. Add the DateAdd function for PHP
6. Add the DateDiff function for PHP
** Get the current date and time.
In Unix, the representation of time is the number of seconds that have elapsed since, January 1, January 1, 1970. This is called a UNIX timestamp (Unix Epoch ).
If we have such a piece of code:
?
Echo time ();
?
Returns 958905820
The time is, January 1, May 21, 2000.
You may say this is pretty good. If this is not helpful to me, or it is only helpful. In PHP, all date-processing functions must use the timestamp value returned by time. At the same time, because PHP uses the same timestamp value in both Unix and Windows systems, this allows you to port between different systems without modifying the code. Another benefit is that the time () function returns an integer. You can store it in the database as an integer or text field without using a special date/time field.
You have basically understood the Unix timestamp value. Now Let's demonstrate its actual usage.
Change the date display mode-date and time display form
PHP provides two methods to convert Unix timestamp values into useful data. The first is the date () function. This function has two parameters-the first string is used to set the format you want to return, and the second is the Unix timestamp value.
The formatted string uses some simple special formatting characters to display the date and time of the format you want to see. Suppose you want the date to display "18h01 Sunday 21 May" in this format ".
We need to use a special formatted character for each part of the string. You can find it in the date and time function library in the PHP Manual. There are a large number of special formatting characters, which are similar to the day of the week, the English name of the month, the year expressed in two or four digits, whether it is morning (AM) or afternoon (PM) and others. For this example, we need the following special characters:
'H'-hour in 24-hour format
'I'-minutes
'L'-full name of the day of the week
'D '-the day of the month
'F'-English full name of the month
Therefore, the formatted string is "Hhi l d F", and the PHP code is:
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.