Get the current date and time - how many ways can we do? 2. Change the way date is displayed - the display format of date and time 3. Convert the current date to Unix timestamp value 4. Change the 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 the current date and time - how many ways do we have?
2. Change the way the date is displayed - the way the date and time are displayed
3. Convert the current date to a 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 tutorial
6. Add datediff function to php
**Get the 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. This is called the 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 tutorial 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 the Unix timestamp value, let's show it in action.
Change the way the date is displayed - the display format of date and time
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
Therefore our format string is "hhi l d f" and the php code is:
?
echo date ("hhi l d f" ,time());
?
When we execute this code, we find that the result we get is:
180609 sunday 21 may
This result seems a bit strange. Let's check the php manual again. It turns out that 'h' represents the hour in the 12-hour clock. This once again proves the truth: "The computer only does what you tell it to do, not what you want it to do." We have two options. The first is to use the escape character "" before h:
echo date ("hhi l d f", time());