Home > Article > Backend Development > Date processing in PHP_PHP tutorial
I'm planning to write a help processing system in PHP. I found that I had to know how long it had been since I dealt with the last customer's problem? Solving this problem was quite simple when I used ASP in the past. ASP has a corresponding function DateDiff that can give the number of months, days and seconds between two dates. When I searched the PHP manual I found that PHP does not have a similar function.
This article contains the following content:
1. Get the current date and time - how many ways do we have?
2. Change the way the date is displayed - the display form of date and time
3. Convert the current date to Unix timestamp value
4. Change date
a. Add time
b. Subtract time
c. Find the interval between two dates
5. Add DateAdd function to PHP
6. Add DateDiff function to PHP
**Get current date and time
In Unix, time is expressed by calculating the number of seconds that have passed since 0:00 on January 1, 1970, which is called a UNIX timestamp (Unix Epoch).
If we have a piece of code like this:
?
echo time();
?
Will return value 958905820
The time at this time was 12:43 on May 21, 2000.
You might say that's pretty good. When this doesn't help me at all, or only a little. In PHP, all date processing functions must use the timestamp value returned by time(). At the same time, since PHP uses the same timestamp value in both Unix and Windows systems, this allows you to port it between different systems without modifying the code. Another benefit is that the time() function returns an integer, which you can store in the database as an integer field or a text field without having to use a special date/time field.
Now that you have a basic understanding of Unix timestamp values, let's show you what they are used for in action.
Change the way the date is displayed - the way the date and time are displayed
PHP provides two methods to convert Unix timestamp values into useful data. The first is the date() function. This function takes two parameters - the first string is used to set the format you wish to return, and the second is the Unix timestamp value.
Format strings use some simple special formatting characters to display the date and time in the format you want to see. Suppose you want the date to be displayed in the format "18h01 Sunday 21 May".
We need to use a special formatting character for each part of the string, which you can find in the date and time function library in the PHP manual. There are a lot of such special formatting characters. They represent the day of the week, the English name of the month, the year represented by 2 or 4 digits, whether it is morning (AM) or afternoon (PM) and others. The special characters we need for this example are:
'H' – hour in 24-hour format
'i'- minutes
'l' - the full English name of the day of the week
'd'-the day of the month
'F' - the full English name of the month
So our format string is "Hhi l d F" and the PHP code is: